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
Zip :: Archive.open_buffer(...)
:require 'zipruby'
Zip::Archive.open_buffer(str) do |archive|
archive.each do |entry|
entry.name
entry.read
end
end
由于Ruby-Zip似乎缺乏读写IO对象的支持,您可以模拟File。
正如@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
zlib库。与StringIO兼容良好。