I have a zip archive in a string, but the rubyzip gem appears to want input from a file. The best I've come up with is to write the zip archive to a tempfile for the sole purpose of passing the filename to Zip::ZipFile.foreach()
, but this seems tortured:
require 'zip/zip'
def unzip(page)
"".tap do |str|
Tempfile.open("unzip") do |tmpfile|
tmpfile.write(page)
Zip::ZipFile.foreach(tmpfile.path()) do |zip_entry|
zip_entry.get_input_stream {|io| str << io.read}
end
end
end
end
Is there a simpler way?
NOTE: See also Ruby Unzip String.
See 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