如何解压缩包含 rails 中文件夹和文件的 zip 文件,同时保持目录结构 [英] How to unzip a zip file containing folders and files in rails, while keeping the directory structure

查看:51
本文介绍了如何解压缩包含 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

这篇关于如何解压缩包含 rails 中文件夹和文件的 zip 文件,同时保持目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆