如何在 Ruby 中遍历内存中的 zip 文件 [英] How to iterate through an in-memory zip file in Ruby

查看:41
本文介绍了如何在 Ruby 中遍历内存中的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个单元测试,其中一个正在返回一个 zip 文件,我想检查这个 zip 文件的内容,从中获取一些值,并将这些值传递给下一个测试.

I am writing a unit test, and one of them is returning a zip file and I want to check the content of this zip file, grab some values from it, and pass the values to the next tests.

我正在使用 Rack Test,所以我知道我的 zip 文件的内容在 last_response.body 中.我已经浏览了 RubyZip 的文档,但它似乎总是在期待一个文件.由于我正在运行单元测试,我更喜欢在内存中完成所有操作,以免测试 zip 文件污染任何文件夹(如果可能).

I'm using Rack Test, so I know the content of my zip file is inside last_response.body. I have looked through the documentation of RubyZip but it seems that it's always expecting a file. Since I'm running a unit test, I prefer to have everything done in the memory as not to pollute any folder with test zip files, if possible.

推荐答案

请参阅@bronson 的答案,以获取使用较新的 RubyZip API 的此答案的最新版本.

See @bronson’s answer for a more up to date version of this answer using the newer RubyZip API.

您链接的 Rubyzip 文档看起来有点旧.最新版本 (0.9.9) 可以处理IO 对象,因此您可以使用 StringIO(稍作调整).

The Rubyzip docs you linked to look a bit old. The latest release (0.9.9) can handle IO objects, so you can use a StringIO (with a little tweaking).

即使 api 会接受 IO,它似乎仍然假设它是一个文件并尝试在它上面调用 path,所以第一个猴子补丁 StringIO 添加一个 path 方法(它实际上不需要做任何事情):

Even though the api will accept an IO, it still seems to assumes it’s a file and tries to call path on it, so first monkey patch StringIO to add a path method (it doesn’t need to actually do anything):

require 'stringio'
class StringIO
  def path
  end
end

然后您可以执行以下操作:

Then you can do something like:

require 'zip/zip'
Zip::ZipInputStream.open_buffer(StringIO.new(last_response.body)) do |io|
  while (entry = io.get_next_entry)
    # deal with your zip contents here, e.g.
    puts "Contents of #{entry.name}: '#{io.read}'"
  end
end

一切都将在内存中完成.

and everything will be done in memory.

这篇关于如何在 Ruby 中遍历内存中的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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