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/

On Tue, Aug 22, 2006 at 04:30:01PM +0900, Erik V. 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/

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

–Aaron

require ‘stringio’
require ‘zlib’

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

That what I ended up doing.

In article [email protected],
Aaron P. [email protected] wrote:

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.