如何解压缩包含 rails 中文件夹和文件的 zip 文件,同时保持目录结构
问题描述:
我需要使用 rails ziprails
gem 解压缩包含许多文件夹和文件的 zip 文件.同时还保持文件和文件夹的组织方式.
I need to extract a zip file that containes many folders and files using rails ziprails
gem. While also keeping the files and folders organized the way they where ziped.
这并不像我一样直接.请参阅我在下面找到的解决方案(添加以供将来参考)
This was not as straight forward as i though. Please see the solution i found beneath (added for future reference)
答
这对我有用.解压缩包含子文件夹和文件的压缩文件夹时,得到与您预期相同的结果.
This worked for me. Gave the same result as you would expect when unzipping a zipped folder with subfolders and files.
Zip::Zip.open(file_path) do |zip_file|
zip_file.each do |f|
f_path = File.join("destination_path", f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
end
end
来自该站点的解决方案:http://bytes.com/topic/ruby/answers/862663-解压缩文件上传红宝石
The solution from this site: http://bytes.com/topic/ruby/answers/862663-unzipping-file-upload-ruby