Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't use zlib to decompress string like gzip's style

Tags:

python

gzip

zlib

I know here has many questions and answers about decompress data with zlib or gzip module in python. But I'm curious about how gzip implement it since gzip is based on zlib.
I read gzip's source and found it's using zlib to decompress data chunk by chunk with wbits set to -15.
However when I directly use zlib with wbits -15 to decompress, it tells me "invalid block type", only with wbits 15+16 it can works.

I know why I should use 15+16, however I don't know why gzip can use -15 but I can't. Who knows the differences of implementations between mine and gzip modules?

like image 520
ayanamist Avatar asked Oct 18 '25 11:10

ayanamist


1 Answers

The zlib module passes the wbits parameter directly the actual zlib library. There it is called windowBits and is documented in the zlib manual. Let me quote the relevant section:

windowBits can also be –8..–15 for raw deflate. In this case, -windowBits determines the window size. deflate() will then generate raw deflate data with no zlib header or trailer, and will not compute an adler32 check value.

Since the gzip module does the header parsing and generation itself, it needs to tell zlib to avoid it. Otherwise there would be two zlib headers and a broken compressed file.

like image 171
Helmut Grohne Avatar answered Oct 19 '25 23:10

Helmut Grohne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!