如何使用 ruby​​zip 解压压缩文件夹

发布于 2024-11-07 16:05:03 字数 982 浏览 1 评论 0原文

我知道如何使用 ruby​​zip 检索普通 zip 文件的内容。但我在解压缩压缩文件夹的内容时遇到了麻烦,我希望你们中的任何人都可以帮助我。

这是我用来解压缩的代码:

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
  logger.debug "#{entry.name}"
  @data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
 end
end

entry.name 为我提供了 zip 文件内文件的名称。这与普通的 zip 文件完美配合。但是,当从文件夹创建 zip 文件时,条目的名称类似于:test-folder/test.pdf。当我尝试创建该文件时,它告诉我找不到该文件。这可能是因为它位于 zip 内的“test”文件夹内。

如果我检查条目是文件夹,则找不到文件夹。所以我认为解决方案是将条目作为流读取,然后将其保存为文件。获取入口流很容易,但是如何将其保存为文件呢?这就是我到目前为止所得到的。

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
  logger.debug "#{entry.name}"
  @data = entry.get_input_stream.read
  # How do i create a file from a stream?
 end
end

基本上我的问题是:如何从流创建文件?或者有比我更简单的方法吗?

===编辑=== 我使用回形针来存储文件。

I know how to retrieve the contents of a normal zip-file with rubyzip. But i got trouble unzipping the contents of a zipped folder and i hope any of u guys can help me out.

this is the code i use to unzip:

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
  logger.debug "#{entry.name}"
  @data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
 end
end

entry.name gives me the name of the file inside the zip-file. This works perfectly with a normal zipfile. But when the zipfile is created from a folder, then the name of the entries are something like: test-folder/test.pdf. When i then try to create the file, it tells me the file can not be found. This is probably because it is inside the "test"-folder that is inside the zip.

If i check the entry to be a folder, no folder can be found. So i thought the solution to be to read the entry as a stream and then save it as a file. It is easy to get the entry-stream, but how do I save it as a file? This is what i got so far.

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
  logger.debug "#{entry.name}"
  @data = entry.get_input_stream.read
  # How do i create a file from a stream?
 end
end

Basically my question is: how can i create a file from a stream? Or is there an easier approach to this than mine?

===EDIT===
I use paperclip to store the files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉鱼一梦 2024-11-14 16:05:03

我发现基于 jhwist 的更简单的方法工作正常:

Zip::File.open(@file_location) do |zipfile|
  zipfile.each do |entry|
    # The 'next if...' code can go here, though I didn't use it
    unless File.exist?(entry.name)
      FileUtils::mkdir_p(File.dirname(entry.name))
      zipfile.extract(entry, entry.name) 
    end
  end
end

条件显然是可选的,但如果没有它,如果代码尝试覆盖现有文件,则会引发错误。

I found that a simpler approach based on jhwist's worked ok:

Zip::File.open(@file_location) do |zipfile|
  zipfile.each do |entry|
    # The 'next if...' code can go here, though I didn't use it
    unless File.exist?(entry.name)
      FileUtils::mkdir_p(File.dirname(entry.name))
      zipfile.extract(entry, entry.name) 
    end
  end
end

The conditional is obviously optional, but without it the code will raise an error if it tries to overwrite an existing file.

没有伤那来痛 2024-11-14 16:05:03

我认为你的问题不在于你是否需要从流中写入文件。基本上,如果你调用 File.new将会创建一个新的 IO-StreamFileIO 的子类)。因此,无论您想对 zip 文件中的流执行什么操作,也应该对常规文件进行操作。

当你说

当我尝试创建文件时,它告诉我找不到该文件

我认为发生的情况是您要创建的文件的父目录不存在(在您的情况下 test-folder< /代码>)。你想做的是类似的事情(未经测试):

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
   next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
   logger.debug "#{entry.name}"
   FileUtils::mkdir_p(File.dirname(entry.name)) # might want to check if it already exists    
   @data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
 end
end

I think your problem is not whether you need to write a file from a stream or not. Basically, if you call File.new it will create a new IO-Stream (File is a subclass of IO). Therefore whatever you want to do with the stream from the zipfile should also work with a regular file.

When you say

When i then try to create the file, it tells me the file can not be found

I think what happens is that the parent-directory for the file you want to create does not exist (in your case the test-folder). What you want to do is something like that (not tested):

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
   next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?
   logger.debug "#{entry.name}"
   FileUtils::mkdir_p(File.dirname(entry.name)) # might want to check if it already exists    
   @data = File.new("#{Rails.root.to_s}/tmp/#{entry.name}")
 end
end
明媚殇 2024-11-14 16:05:03

我通过使用流并创建 StringIO 解决了这个问题。这是代码

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?

  begin
   # the normal unzip-code
  rescue Errno::ENOENT
   # when the entry can not be found
   @data = entry.get_input_stream.read
   @file = StringIO.new(@data)
   @file.class.class_eval { attr_accessor :original_filename, :content_type }
   @file.original_filename = entry.name
   @file.content_type = MIME::Types.type_for(entry.name)

   # save it / whatever
  end
 end
end

I solved it by using a stream and creating a StringIO. Here is the code

Zip::ZipFile::open(@file_location) do |zip|
 zip.each do |entry|
  next if entry.name =~ /__MACOSX/ or entry.name =~ /\.DS_Store/ or !entry.file?

  begin
   # the normal unzip-code
  rescue Errno::ENOENT
   # when the entry can not be found
   @data = entry.get_input_stream.read
   @file = StringIO.new(@data)
   @file.class.class_eval { attr_accessor :original_filename, :content_type }
   @file.original_filename = entry.name
   @file.content_type = MIME::Types.type_for(entry.name)

   # save it / whatever
  end
 end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文