Unzipping a gzipped string

I'm trying to unzip a string I have in memory, but unfortunately I keep
getting a 'incorrect header check (Zlib::DataError)' when I try to unzip
the string.

The weird thing is that I can write the string out to a file, and unzip
it using gzip or even Zlib::GzipReader.

For example, using the same file, this works:

  require 'zlib'

  File.open(ARGV[0], 'r') { |f|
    gz = Zlib::GzipReader.new(f)
    puts gz.read
  }

But this doesn't (it fails with 'incorrect header check'):

  require 'zlib'

  File.open(ARGV[0], 'r') { |f|
    puts Zlib::Inflate.inflate(f.read)
  }

Anyone have any hints? Thanks.

--Aaron

You might have to use binary files.

File.open(ARGV[0], 'rb')

Windows differentiates between binary and text files.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

In article <20060822054708.GA8197@eviladmins.lan>,

···

Aaron Patterson <aaron_patterson@speakeasy.net> wrote:

I'm trying to unzip a string I have in memory, but unfortunately I keep
getting a 'incorrect header check (Zlib::DataError)' when I try to unzip
the string.

The weird thing is that I can write the string out to a file, and unzip
it using gzip or even Zlib::GzipReader.

For example, using the same file, this works:

require 'zlib'

File.open(ARGV[0], 'r') { |f|
   gz = Zlib::GzipReader.new(f)
   puts gz.read
}

But this doesn't (it fails with 'incorrect header check'):

require 'zlib'

File.open(ARGV[0], 'r') { |f|
   puts Zlib::Inflate.inflate(f.read)
}

Anyone have any hints? Thanks.

A Gzip file has a header which includes the original filename. A
zlib-compressed string doesn't have the same header.
--
Mark Ping
emarkp@soda.CSUA.Berkeley.EDU

I'm on OS X. I tried it with 'rb', but still get the same error.

--Aaron

···

On Tue, Aug 22, 2006 at 04:30:01PM +0900, Erik Veenstra wrote:

You might have to use binary files.

File.open(ARGV[0], 'rb')

Windows differentiates between binary and text files.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

require 'stringio'
require 'zlib'

gz = Zlib::GzipReader.new( StringIO.new( string ) )
puts gz.read

That what I ended up doing.

···

--
Mark Van Holstyn
mvette13@gmail.com
http://lotswholetime.com