Reputation: 1173
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?
Upvotes: 4
Views: 4422
Reputation: 6778
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.
Upvotes: 3