Ruby Unzip String

unzipping a zip archive from a 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

How to decompress Gzip string in ruby?

The above method didn't work for me.

I kept getting incorrect header check (Zlib::DataError) error. Apparently it assumes you have a header by default, which may not always be the case.

The work around that I implemented was:

require 'zlib'
require 'stringio'
gz = Zlib::GzipReader.new(StringIO.new(resp.body.to_s))
uncompressed_string = gz.read

Ruby: Download zip file and extract

I found the solution myself and then at stackoverflow :D (How to iterate through an in-memory zip file in Ruby)

input = HTTParty.get("http://example.com/somedata.zip").body
Zip::InputStream.open(StringIO.new(input)) do |io|
while entry = io.get_next_entry
puts entry.name
parse_zip_content io.read
end
end
  1. Download your ZIP file, I'm using HTTParty for this (but you could also use ruby's open command (require 'open-uri').
  2. Convert it into a StringIO stream using StringIO.new(input)
  3. Iterate over every entry inside the ZIP archive using io.get_next_entry (it returns an instance of Entry)
  4. With io.read you get the content, and with entry.name you get the filename.

How to iterate through an in-memory zip file in Ruby

See @bronson’s answer for a more up to date version of this answer using the newer RubyZip API.

The Rubyzip docs you linked to look a bit old. The latest release (0.9.9) can handle IO objects, so you can use a StringIO (with a little tweaking).

Even though the api will accept an IO, it still seems to assumes it’s a file and tries to call path on it, so first monkey patch StringIO to add a path method (it doesn’t need to actually do anything):

require 'stringio'
class StringIO
def path
end
end

Then you can do something like:

require 'zip/zip'
Zip::ZipInputStream.open_buffer(StringIO.new(last_response.body)) do |io|
while (entry = io.get_next_entry)
# deal with your zip contents here, e.g.
puts "Contents of #{entry.name}: '#{io.read}'"
end
end

and everything will be done in memory.

Trying to read files in a zip archive without extracting them

According to the rubyzip documentation (and the question you linked) mZip should be of class String and contain the path to a file rather than File or Zip::Entry.

mZip = './folder/file.zip'
def read_file
Zip::File.open(myZip) do |zip_file|
#...
end

extracting strings from an array of mixed data! (ruby)

Another way:

[                 # Wrap the entire thing in an array.
mixed_data_1.
flatten. # Recursively flatten the nested arrays.
grep(String). # Filter out only strings.
join(' ') # Join the resulting strings together with a space.
]

However, when presented with such a problem, it is almost always better to fix the problem at the source and ensure that you don't get such "weird" data structures in the first place. Ruby is an object-oriented language, not an arrays-of-arrays-of-integers-or-strings-oriented language, one should generally manipulate rich, structured, "live" objects, not anemic, unstructured, "dead" data (or more precisely, those objects should manipulate themselves).

How to unzip a file with ruby

The problem with installing the gem that way is that you're shelling out to another process with:

`gem install rubyzip`

and after that finishes installing the gem, your current irb session still won't see it. You'd have to reload irb with exec "irb" and then calling require 'zip' again.

Note: those are backticks not single quotes.

Try this:

begin
require 'zip'
rescue LoadError
`gem install rubyzip`
exec "irb"
retry
end

For me require 'zip' works. I have rubyzip-1.1.2

Now you should be able to use Zip

Also, the gem command is rubygems. So you can't install rubygems with itself. It should already be installed, but if not try this: http://rubygems.org/pages/download



Related Topics



Leave a reply



Submit