Most simple usage of zlib or pr-zlib

HI *, I'm trying to unzip a file programmatically, and having lots of difficulties.

ruby-1.9.2-p136 :009 > data = StringIO.new( open( 'test.zip', 'r' ).read )
=> #<StringIO:0x00000100c4c8a8>
ruby-1.9.2-p136 :010 > Zlib::GzipReader.new(data).read
Zlib::GzipFile::Error: not in gzip format

This is with both zlib in stdlib and pr-zlib from github.
What's strange is that the file seems in the right format:

[ngw@slicingupeyeballs:~]$ file test.zip
test.zip: Zip archive data, at least v1.0 to extract

What I need is a supersimple solution to extract a zip file and retain the same directory structure, no more no less :slight_smile:

Someone worked on something like this before ?

  ngw

Hi,

ruby-1.9.2-p136 :009 > data = StringIO.new( open( 'test.zip', 'r' ).read )

I see that you want to read a ZIP format file, "test.zip". Right?

ruby-1.9.2-p136 :010 > Zlib::GzipReader.new(data).read
Zlib::GzipFile::Error: not in gzip format

However, you used Zlib::GzipReader. This class is used for not zip format,
but gzip format [1], so that error occurred.

One way to unzip a file is to use zipruby gem [2]. Please try it.

[1] http://www.ruby-doc.org/stdlib/libdoc/zlib/rdoc/classes/Zlib/GzipReader.html
[2] search | RubyGems.org | your community gem host

Regards,

···

--
NOBUOKA Yu
http://www.vividcode.info/

You might also want to take a look at the archive-zip gem:

http://rubygems.org/gems/archive-zip

Support for Ruby 1.9 is still in the works, but it should do what you
want with minimal effort otherwise.

-Jeremy

···

On 3/9/2011 06:14, Y. NOBUOKA wrote:

One way to unzip a file is to use zipruby gem [2]. Please try it.

Thank you guys, I solved with zipruby.
Unfortunately I'm on 1.9, but does archive-zip support both zip and gzip ?

  ngw

···

On 2011-03-09 15:36:38 +0100, Jeremy Bopp said:

On 3/9/2011 06:14, Y. NOBUOKA wrote:

One way to unzip a file is to use zipruby gem [2]. Please try it.

You might also want to take a look at the archive-zip gem:

archive-zip | RubyGems.org | your community gem host

Support for Ruby 1.9 is still in the works, but it should do what you
want with minimal effort otherwise.

No, it does not and will not. While the two technologies share the same
underlying compression codec, they each use the codec quite differently.
A ZIP file is an archive of one or more files and directories each
individually compressed, or possibly not compressed at all. A GZIP file
is a compressed version of a single file's contents. Those contents
could be, and often are, that of a TAR file which itself contains one or
more uncompressed files and directories; however, this method of
archival is completely different than that of a ZIP file.

If you want to extract an actual GZIP file, take a look at
Zlib::GzipReader included with the standard library:

http://rdoc.info/stdlib/zlib/1.9.2/Zlib/GzipReader

-Jeremy

···

On 03/09/2011 06:50 PM, Nicholas Wieland wrote:

Unfortunately I'm on 1.9, but does archive-zip support both zip and gzip ?