4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unzip from StringIO, Using Rubyzip

Last updated at Posted at 2013-11-27

Requires Rubyzip 1.1.0+

Given a data of Zip file that is read from an IO, extract that data into a Hash.

def unzip(data)
  fin = StringIO.new(data)

  entries = {}

  ::Zip::InputStream.open(fin) do |fzip|
    while entry = fzip.get_next_entry
      entries[entry.name] = fzip.read
    end
  end

  entries
end

Example:

entries = unzip(open("http://example.com/archive.zip").read)
entries["file.txt"] #=> "Hello World\n" (File Content)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?