You have this error:

zlib.error: Error -3 while decompressing: incorrect header check

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 than RFC 1950 (zlib compressed format) or RFC 1952 (gzip compressed format).

choosing windowBits

But zlib can decompress all those formats:

  • to (de-)compress deflate format, use wbits = -zlib.MAX_WBITS
  • to (de-)compress zlib format, use wbits = zlib.MAX_WBITS
  • to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16

See documentation in http://www.zlib.net/manual.html#Advanced (section inflateInit2)

examples

test data:

>>> deflate_compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
>>> zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS)
>>> gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
>>> 
>>> text = '''test'''
>>> deflate_data = deflate_compress.compress(text) + deflate_compress.flush()
>>> zlib_data = zlib_compress.compress(text) + zlib_compress.flush()
>>> gzip_data = gzip_compress.compress(text) + gzip_compress.flush()
>>> 

obvious test for zlib:

>>> zlib.decompress(zlib_data)
'test'

test for deflate:

>>> zlib.decompress(deflate_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
>>> zlib.decompress(deflate_data, -zlib.MAX_WBITS)
'test'

test for gzip:

>>> zlib.decompress(gzip_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
>>> zlib.decompress(gzip_data, zlib.MAX_WBITS|16)
'test'

the data is also compatible with gzip module:

>>> import gzip
>>> import StringIO
>>> fio = StringIO.StringIO(gzip_data)  # io.BytesIO for Python 3
>>> f = gzip.GzipFile(fileobj=fio)
>>> f.read()
'test'
>>> f.close()

automatic header detection (zlib or gzip)

adding 32 to windowBits will trigger header detection

>>> zlib.decompress(gzip_data, zlib.MAX_WBITS|32)
'test'
>>> zlib.decompress(zlib_data, zlib.MAX_WBITS|32)
'test'

using gzip instead

or you can ignore zlib and use gzip module directly; but please remember that under the hood, gzip uses zlib.

fh = gzip.open('abc.gz', 'rb')
cdata = fh.read()
fh.close()
Answer from dnozay on Stack Overflow
Top answer
1 of 9
167

You have this error:

zlib.error: Error -3 while decompressing: incorrect header check

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 than RFC 1950 (zlib compressed format) or RFC 1952 (gzip compressed format).

choosing windowBits

But zlib can decompress all those formats:

  • to (de-)compress deflate format, use wbits = -zlib.MAX_WBITS
  • to (de-)compress zlib format, use wbits = zlib.MAX_WBITS
  • to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16

See documentation in http://www.zlib.net/manual.html#Advanced (section inflateInit2)

examples

test data:

>>> deflate_compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
>>> zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS)
>>> gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
>>> 
>>> text = '''test'''
>>> deflate_data = deflate_compress.compress(text) + deflate_compress.flush()
>>> zlib_data = zlib_compress.compress(text) + zlib_compress.flush()
>>> gzip_data = gzip_compress.compress(text) + gzip_compress.flush()
>>> 

obvious test for zlib:

>>> zlib.decompress(zlib_data)
'test'

test for deflate:

>>> zlib.decompress(deflate_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
>>> zlib.decompress(deflate_data, -zlib.MAX_WBITS)
'test'

test for gzip:

>>> zlib.decompress(gzip_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check
>>> zlib.decompress(gzip_data, zlib.MAX_WBITS|16)
'test'

the data is also compatible with gzip module:

>>> import gzip
>>> import StringIO
>>> fio = StringIO.StringIO(gzip_data)  # io.BytesIO for Python 3
>>> f = gzip.GzipFile(fileobj=fio)
>>> f.read()
'test'
>>> f.close()

automatic header detection (zlib or gzip)

adding 32 to windowBits will trigger header detection

>>> zlib.decompress(gzip_data, zlib.MAX_WBITS|32)
'test'
>>> zlib.decompress(zlib_data, zlib.MAX_WBITS|32)
'test'

using gzip instead

or you can ignore zlib and use gzip module directly; but please remember that under the hood, gzip uses zlib.

fh = gzip.open('abc.gz', 'rb')
cdata = fh.read()
fh.close()
2 of 9
4

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.

import gzip
f = gzip.open('/home/joe/file.txt.gz', 'rb')
file_content = f.read()
f.close()
🌐
GitHub
github.com › pyinstaller › pyinstaller › issues › 2247
"zlib.error: Error -3 while decompressing data: incorrect header check" when trying to run the exe which is encrypted with pyinstaller. · Issue #2247 · pyinstaller/pyinstaller
November 1, 2016 - "zlib.error: Error -3 while decompressing data: incorrect header check" when trying to run the exe which is encrypted with pyinstaller.#2247
Author   pyinstaller
Discussions

Error -3 while decompressing data: incorrect header check
I/python (31934): Full traceback: I/python (31934): File "renpy/bootstrap.py", line 265, in bootstrap I/python (31934): File "renpy/main.py", line 226, in main I/python (31934): File "renpy/script.... More on github.com
🌐 github.com
2
December 15, 2013
[BUG] `Error -3 while decompressing data: incorrect header check`
$ pyarmor gen -O obfdist --pack ... INFO Python 3.11.2 INFO Pyarmor 8.2.9 (trial), 000000, non-profits INFO Platform windows.x86_64 INFO implicitly set output to ".pyarmor\pack\dist" INFO extracting bundle ".\dist\foo.exe" ERROR Error -3 while decompressing data: incorrect header ... More on github.com
🌐 github.com
5
July 7, 2023
python - Error -3 while decompressing data: incorrect header check - urllib2 - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most · Connect and share knowledge within a single location that is structured and easy to search More on stackoverflow.com
🌐 stackoverflow.com
October 21, 2014
python - error: Error -3 while decompressing data: incorrect header check (Error occuring which decompressing base64 encoded string) - Stack Overflow
The below is successfully working code in example 1: Example 1: import zlib import base64 data = " More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 49755011 › zlib-error-error-3-while-decompressing-data-incorrect-header-check
python - zlib.error: Error -3 while decompressing data: incorrect header check - Stack Overflow
April 10, 2018 - import zlib import base64 b = "kxcp+3ky2c95u2rhHvGq6t12rh64MhjvC3tujkONnrQ4VIh2jr3FlLudiQJzeTLN" print(zlib.decompress(base64.b64decode(b))) Traceback (most recent call last): File "/home/lcardoso/Projetos/stalkers/busca_nfe_incompleta/teste.py", line 8, in <module> print(zlib.decompress(base64.b64decode(b))) zlib.error: Error -3 while decompressing data: incorrect header check
🌐
GitHub
github.com › renpy › renpy › issues › 258
Error -3 while decompressing data: incorrect header check · Issue #258 · renpy/renpy
December 15, 2013 - I/python (31934): Full traceback: I/python (31934): File "renpy/bootstrap.py", line 265, in bootstrap I/python (31934): File "renpy/main.py", line 226, in main I/python (31934): File "renpy/script.py", line 177, in load_script I/python (31934): File "renpy/script.py", line 418, in load_appropriate_file I/python (31934): File "renpy/script.py", line 310, in load_file I/python (31934): File "renpy/script.py", line 288, in load_file_core I/python (31934): File "/home/tom/ab/android/python-for-android/build/python-install/lib/python2.7/encodings/zlib_codec.py", line 43, in zlib_decode I/python (31934): error: Error -3 while decompressing data: incorrect header check
Author   renpy
🌐
GitHub
github.com › dashingsoft › pyarmor › issues › 1355
[BUG] `Error -3 while decompressing data: incorrect header check` · Issue #1355 · dashingsoft/pyarmor
July 7, 2023 - $ pyarmor gen -O obfdist --pack .\dist\foo.exe .\foo.py INFO Python 3.11.2 INFO Pyarmor 8.2.9 (trial), 000000, non-profits INFO Platform windows.x86_64 INFO implicitly set output to ".pyarmor\pack\dist" INFO extracting bundle ".\dist\foo.exe" ERROR Error -3 while decompressing data: incorrect header check
Author   dashingsoft
Top answer
1 of 2
2

You may have any number of reasons why you choose httplib2, but it's far too easy to get the status code of an HTTP request using the python module requests. Install with the following command:

$ pip install requests

See an extremely simple example below.

In [1]: import requests as rq

In [2]: url = "http://www.sueddeutsche.de/wirtschaft/deutschlands-innovationsangst-wir-neobiedermeier-1.2117528"

In [3]: r = rq.get(url)

In [4]: r
Out[4]: <Response [200]>

Link

Unless you have a considerable constraint that needs httplib2 explicitly, this solves your problem.

2 of 2
1

This may be a bug (or just uncommon design decision) in httplib2. I don't get this problem with urllib2 or httplib in the 2.x stdlib, or urllib.request or http.client in the 3.x stdlib, or the third-party libraries requests, urllib3, or pycurl.

So, is there a reason you need to use this particular library?

If so:

I actually need only http status, why is it reading the whole content?

Well, most HTTP libraries are going to read and parse the whole content, or at least the headers, before returning control. That way they can respond to simple requests about the headers or chunked encoding or MIME envelope or whatever without any delay.

Also, many of them automate things like 100 continue, 302 redirect, various kinds of auth, etc., and there's no way they could do that if they didn't read ahead. In particular, according to the description for httplib2, handling these things automatically is one of the main reasons you should use it in the first place.

Also, the first TCP read is nearly always going to include the headers anyway, so why not read them?

This means that if the headers are invalid, you may get an exception immediately. They may still provide a way to get the status code (or the raw headers, or other information) anyway.


As a side note, if you only want the HTTP status, you should probably send a HEAD request rather than a GET. Unless you're writing and testing a server, you can almost always rely on the fact that, as the RFC says, the status and headers should be identical to what you'd get with GET. In fact, that would almost certainly solve things in this case—if there is no body to decompress, the fact that httplib2 has gotten confused into thinking the body is gzipped when it isn't won't matter anyway.

🌐
Stack Overflow
stackoverflow.com › questions › 41872219 › zlib-python-error-error-3-while-decompressing-data-incorrect-header-check
zlib python error: Error -3 while decompressing data: incorrect header check - Stack Overflow
January 26, 2017 - The essence of the question is, has mysql database it is compressed using zlib data. When unpacking the machine where they are compressed all ok. On another machine zlib.error: Error -3 while
Find elsewhere
🌐
Lemma Soft
lemmasoft.renai.us › forums › viewtopic.php
Error -3 while decompressing data: incorrect header check - Lemma Soft Forums
File "H:\Ren'py\renpy-6.16.2-sdk\renpy\script.py", line 177, in load_script self.load_appropriate_file(".rpyc", ".rpy", dir, fn, initcode) File "H:\Ren'py\renpy-6.16.2-sdk\renpy\script.py", line 436, in load_appropriate_file if self.load_file(dir, fn + compiled, initcode): File "H:\Ren'py\renpy-6.16.2-sdk\renpy\script.py", line 310, in load_file data, stmts = self.load_file_core(dir, fn) File "H:\Ren'py\renpy-6.16.2-sdk\renpy\script.py", line 288, in load_file_core data, stmts = loads(f.read().decode('zlib')) File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/encodings/zlib_codec.py", line 43, in zlib_decode error: Error -3 while decompressing data: incorrect header check Windows-post2008Server-6.2.9200 Ren'Py 6.16.2.490 The game will launch just fine in Ren'py 6.99.10 and above.
🌐
Python
bugs.python.org › file28419 › zlib_tests_pep8.patch
Python
@@ -170,8 +169,9 @@ # An useful error message is given x = zlib.compress(HAMLET_SCENE) self.assertRaisesRegex(zlib.error, - "Error -5 while decompressing data: incomplete or truncated stream", - zlib.decompress, x[:-1]) + "Error -5 while decompressing data: incomplete" + " or truncated stream", + zlib.decompress, x[:-1]) # Memory use of the following functions takes into account overallocation @@ -205,7 +205,7 @@ co = zlib.compressobj() x1 = co.compress(data) x2 = co.flush() - self.assertRaises(zlib.error, co.flush) # second flush should not work + self.assertRaises(zlib.error, co.flush) # sec
🌐
GitHub
github.com › seomoz › reppy › issues › 57
error while decompressing data: incorrect header check · Issue #57 · seomoz/reppy
February 9, 2017 - Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/response.py", line 262, in _decode data = self._decoder.decompress(data) File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/response.py", line 62, in decompress return self._obj.decompress(data) zlib.error: Error -3 while decompressing data: incorrect header check During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "reppy/robots.pyx", line 78, in reppy.robots.FetchMethod (reppy/r
Author   seomoz
🌐
GitHub
github.com › pmaupin › pdfrw › issues › 116
decompressing error: pdfrw:Error -3 while decompressing data: incorrect header check (xx, 0) · Issue #116 · pmaupin/pdfrw
October 10, 2017 - [ERROR] uncompress.py:80 Error -3 while decompressing data: incorrect header check (3, 0) ERROR:pdfrw:Error -3 while decompressing data: incorrect header check (3, 0)
Author   pmaupin
🌐
Jython
bugs.jython.org › issue2433
Issue 2433: zlib decompressobj index error when using negative wbits - Jython tracker
Created on 2015-11-23.10:38:23 by lukasa, last changed 2018-04-13.18:21:39 by psykiatris · Supported by Python Software Foundation, Powered by Roundup
Top answer
1 of 1
2

I managed to decompress it in Python by specifying the decompression dictionary:

#!/usr/bin/python3

import zlib
import base64

header = ("783fe3c6a7c2003b01c4fe00000009000000073a6d6574686f"
"6400000003474554000000053a70617468000000012f000000083a76657273696f6e000000084"
"85454502f312e31000000053a686f73740000000d3139322e3136382e302e313734000000073a"
"736368656d650000000568747470730000000a757365722d6167656e74000000414d6f7a696c6"
"c612f352e30202857696e646f7773204e5420352e313b2072763a33302e3029204765636b6f2f"
"32303130303130312046697265666f782f33302e30000000066163636570740000003f7465787"
"42f68746d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c6170706c69636174696f"
"6e2f786d6c3b713d302e392c2a2f2a3b713d302e380000000f6163636570742d6c616e6775616"
"7650000000e656e2d55532c656e3b713d302e3500000003646e740000000131000000ffff")

zdict = base64.b16decode("000000076F7074696F6E730000000468656164000000"
"04706F7374000000037075740000000664656C65746500000005747261636500000006"
"6163636570740000000E6163636570742D636861727365740000000F6163636570742D"
"656E636F64696E670000000F6163636570742D6C616E67756167650000000D61636365"
"70742D72616E6765730000000361676500000005616C6C6F770000000D617574686F72"
"697A6174696F6E0000000D63616368652D636F6E74726F6C0000000A636F6E6E656374"
"696F6E0000000C636F6E74656E742D6261736500000010636F6E74656E742D656E636F"
"64696E6700000010636F6E74656E742D6C616E67756167650000000E636F6E74656E74"
"2D6C656E67746800000010636F6E74656E742D6C6F636174696F6E0000000B636F6E74"
"656E742D6D64350000000D636F6E74656E742D72616E67650000000C636F6E74656E74"
"2D74797065000000046461746500000004657461670000000665787065637400000007"
"657870697265730000000466726F6D00000004686F73740000000869662D6D61746368"
"0000001169662D6D6F6469666965642D73696E63650000000D69662D6E6F6E652D6D61"
"7463680000000869662D72616E67650000001369662D756E6D6F6469666965642D7369"
"6E63650000000D6C6173742D6D6F646966696564000000086C6F636174696F6E000000"
"0C6D61782D666F72776172647300000006707261676D610000001270726F78792D6175"
"7468656E7469636174650000001370726F78792D617574686F72697A6174696F6E0000"
"000572616E676500000007726566657265720000000B72657472792D61667465720000"
"000673657276657200000002746500000007747261696C6572000000117472616E7366"
"65722D656E636F64696E6700000007757067726164650000000A757365722D6167656E"
"74000000047661727900000003766961000000077761726E696E67000000107777772D"
"61757468656E746963617465000000066D6574686F6400000003676574000000067374"
"6174757300000006323030204F4B0000000776657273696F6E00000008485454502F31"
"2E310000000375726C000000067075626C69630000000A7365742D636F6F6B69650000"
"000A6B6565702D616C697665000000066F726967696E31303031303132303132303232"
"3035323036333030333032333033333034333035333036333037343032343035343036"
"3430373430383430393431303431313431323431333431343431353431363431373530"
"32353034353035323033204E6F6E2D417574686F726974617469766520496E666F726D"
"6174696F6E323034204E6F20436F6E74656E74333031204D6F766564205065726D616E"
"656E746C7934303020426164205265717565737434303120556E617574686F72697A65"
"6434303320466F7262696464656E343034204E6F7420466F756E6435303020496E7465"
"726E616C20536572766572204572726F72353031204E6F7420496D706C656D656E7465"
"64353033205365727669636520556E617661696C61626C654A616E20466562204D6172"
"20417072204D6179204A756E204A756C204175672053657074204F6374204E6F762044"
"65632030303A30303A3030204D6F6E2C205475652C205765642C205468752C20467269"
"2C205361742C2053756E2C20474D546368756E6B65642C746578742F68746D6C2C696D"
"6167652F706E672C696D6167652F6A70672C696D6167652F6769662C6170706C696361"
"74696F6E2F786D6C2C6170706C69636174696F6E2F7868746D6C2B786D6C2C74657874"
"2F706C61696E2C746578742F6A6176617363726970742C7075626C6963707269766174"
"656D61782D6167653D677A69702C6465666C6174652C73646368636861727365743D75"
"74662D38636861727365743D69736F2D383835392D312C7574662D2C2A2C656E713D30"
"2E")

header = base64.b16decode(header.upper())

z = zlib.decompressobj(zdict=zdict)
print(z.decompress(bytearray(header)))
🌐
Python
bugs.python.org › issue224981
Issue 224981: zlib decompress of sync-flushed data fails - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/33564
🌐
GitHub
github.com › apigee › trireme › issues › 181
incorrect header check when zlib decompression in http · Issue #181 · apigee/trireme
March 16, 2018 - In some cases incorrect header check is thrown when decompressing gzip http(s) data. We noticed this when using superagent node_module as it sends Accept-Encoding: gzip,deflate by default. Working ...
Author   apigee
🌐
GitHub
github.com › sentry-extensions › cyclops › issues › 22
error: Error -3 while decompressing data: incorrect header check · Issue #22 · sentry-extensions/cyclops
June 9, 2016 - 2016-06-09 11:25:57,150 - tornado.application - ERROR - Uncaught exception POST /api/22/store/ (10.26.10.38) HTTPRequest(protocol='http', host='prod-vm-sentry-cyclops-01', method='POST', uri='/api/22/store/', version='HTTP/1.1', remote_ip='10.26.10.38', headers={'Content-Length': '261', 'Accept-Encoding': 'identity', 'Content-Encoding': 'deflate', 'Connection': 'close', 'X-Sentry-Auth': 'Sentry sentry_timestamp=1465485957.17, sentry_client=raven-python/5.8.1, sentry_version=6, sentry_key=xxxxxxxxxxxxxxxxx, sentry_secret=yyyyyyyyyyyyyyyyyy', 'User-Agent': 'raven-python/5.8.1', 'Host': 'prod-vm-
Author   sentry-extensions
🌐
GitHub
github.com › piskvorky › smart_open › issues › 108
error: Error -3 while decompressing: incorrect header check · Issue #108 · piskvorky/smart_open
March 10, 2017 - error: Error -3 while decompressing: incorrect header check#108 · Copy link · yg37 · opened · on Mar 10, 2017 · Issue body actions · I was trying to read a compressed file from s3 on ec2 and it did not print any line · for myKey,content in smart_open.s3_iter_bucket(email_bucket): with smart_open.smart_open(myKey) as data: for line in data: print line ·
Author   piskvorky