答案 0 :(得分:4)
rubyzip supports StringIO since version 1.1.0
require "zip"
# zip is the string with the zipped contents
Zip::InputStream.open(StringIO.new(zip)) do |io|
while (entry = io.get_next_entry)
puts "#{entry.name}: '#{io.read}'"
end
end
答案 1 :(得分:2)
请参阅Zip/Ruby Zip::Archive.open_buffer(...)
:
require 'zipruby'
Zip::Archive.open_buffer(str) do |archive|
archive.each do |entry|
entry.name
entry.read
end
end
答案 2 :(得分:1)
由于Ruby-Zip似乎缺乏对IO对象的读/写支持,因此可以伪造File。 你能做的是以下几点:
答案 3 :(得分:0)
正如@Roman提到的,rubyzip目前缺乏对IO对象的读写(包括StringIO.new(s)
)。请尝试使用zipruby,如下所示:
gem install zipruby
require 'zipruby'
# Given a string in zip format, return a hash where
# each key is an zip archive entry name and each
# value is the un-zipped contents of the entry
def unzip(zipfile)
{}.tap do |h|
Zip::Archive.open_buffer(zipfile) do |archive|
archive.each {|entry| h[entry.name] = entry.read }
end
end
end
答案 4 :(得分:-1)
zlib库。适用于StringIO。