Reputation: 7761
I have to work with a zipped (regular Zip) string in Ruby. Apparently I can't save a temporary file with Ruby-Zip or Zip-Ruby.
Is there any practicable way to unzip this string?
Upvotes: 6
Views: 5246
Reputation: 1317
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
Upvotes: 8
Reputation: 35239
As @Roman mentions, rubyzip currently lacks reading and writing of IO objects (including StringIO.new(s)
). Try using zipruby instead, like this:
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
Upvotes: 0
Reputation: 156682
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
Upvotes: 2
Reputation: 13058
As the Ruby-Zip seems to lack support of reading/writing to IO objects, you can fake File. What you can do is the following:
Upvotes: 1