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
- Download your ZIP file, I'm using HTTParty for this (but you could also use ruby's
open
command (require 'open-uri'
). - Convert it into a
StringIO
stream usingStringIO.new(input)
- Iterate over every entry inside the ZIP archive using
io.get_next_entry
(it returns an instance ofEntry
) - With
io.read
you get the content, and withentry.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
In Ruby, How to Get Instance Variables in a Hash Instead of an Array
Why Does Ruby Builder::Xmlmarkup Add Inspect Tag to Xml
How to Override [] Brackets in Ruby
Is It Ever Necessary to Use 'Chomp' Before Using 'To_I' or 'To_F'
Capistrano and API Keys in Env Variables
How to Get Http Headers Before Downloading with Ruby's Openuri
Undefined Method (Nomethoderror) Ruby
How to Do String Comparison in Ruby
How to Get Long Filename from Argv
Execjs and Could Not Find a JavaScript Runtime
Rails 3.1 Load CSS in Particular Order
How to Use Reference Images in SASS When Using Rails 3.1
Gem Install Cannot Find a Header File
How Does Inheritance Work in Ruby
Is There a Ruby Method That Just Returns the Value of a Block
Redirect_Uri_Mismatch. Login with Google Using Ruby on Rails
Result of 'Rake Routes' in Ruby Script
Rails ( Set_No_Cache Method) Cannot Disable Browser Caching in Safari and Opera