I have a gzip file and I am trying to read it via Python as below:
import zlib
do = zlib.decompressobj(16+zlib.MAX_WBITS)
fh = open('abc.gz', 'rb')
cdata = fh.read()
fh.close()
data = do.decompress(cdata)
it throws this error:
zlib.error: Error -3 while decompressing: incorrect header check
How can I overcome it?
Funnily enough, I had that error when trying to work with the Stack Overflow API using Python.
I managed to get it working with the
GzipFile
object from the gzip directory, roughly like this:I just solved the "incorrect header check" problem when uncompressing gzipped data.
You need to set -WindowBits => WANT_GZIP in your call to inflateInit2 (use the 2 version)
Yes, this can be very frustrating. A typically shallow reading of the documentation presents Zlib as an API to Gzip compression, but by default (not using the gz* methods) it does not create or uncompress the Gzip format. You have to send this non-very-prominently documented flag.
Just add headers 'Accept-Encoding': 'identity'
https://github.com/requests/requests/issues/3849
My case was do decompress email messages that are stored in Bullhorn database. The snippet is the following:
Update: dnozay's answer explains the problem and should be the accepted answer.
Try the
gzip
module, code below is straight from the python docs.You have this error:
Which is most likely because you are trying to check headers that are not there, e.g. your data follows
RFC 1951
(deflate
compressed format) rather thanRFC 1950
(zlib
compressed format) orRFC 1952
(gzip
compressed format).choosing windowBits
But
zlib
can decompress all those formats:deflate
format, usewbits = -zlib.MAX_WBITS
zlib
format, usewbits = zlib.MAX_WBITS
gzip
format, usewbits = zlib.MAX_WBITS | 16
See documentation in http://www.zlib.net/manual.html#Advanced (section
inflateInit2
)examples
test data:
obvious test for
zlib
:test for
deflate
:test for
gzip
:the data is also compatible with
gzip
module:automatic header detection (zlib or gzip)
adding
32
towindowBits
will trigger header detectionusing
gzip
insteador you can ignore
zlib
and usegzip
module directly; but please remember that under the hood,gzip
useszlib
.