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 › nodeca › pako › issues › 256
Incorrect header check with zlib header · Issue #256 · nodeca/pako
June 12, 2022 - It appears to have a 2-byte zlib header (78 DA). From my reading through the docs and the code, it seems that by default pako.inflate() should auto-detect whether there's a zlib header as opposed to a gzip header. However, I get an incorrect header check error when doing pako.inflate().
Author   nodeca
🌐
Narkive
comp.compression.narkive.com › Zgr8CeqI › zlib-inflate-returns-invalid-header-check-help-please
Zlib inflate() returns "invalid header check" - help please....
Post by c***@poczta.onet.pl So I thought that after 26 + 7 bytes should be the beginning of zlib stream but it isn't As I said, the compressed data is not a zlib stream -- it is a raw deflate stream. Please read zlib.h for how to perform a raw inflate. Mark Mark, as you said I used inflateInit2() instead of inflateInit() and now all works fine.
🌐
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
🌐
GitHub
github.com › serverless › serverless › discussions › 11211
incorrect header check, can not decompress with nodejs zlib · serverless/serverless · Discussion #11211
const serverless = require("serverless-http"); const express = require("express"); const { gunzip } = require("node:zlib"); const zlib = require("node:zlib"); const { promisify } = require("node:util"); const compression = require("compression"); const fs = require("fs"); const path = require("path"); const app = express(); app.use(express.json()); app.use(compression()); const do_unzip = promisify(gunzip); app.get("/", (req, res, next) => { return res.status(200).json({ message: "working lambda express", }); }); function checkHeadersMiddleware(req, res, next) { const isAccepted = req.headers[
Author   serverless
🌐
Ruby
bugs.ruby-lang.org › issues › 11268
Bug #11268: Zlib::DataError: incorrect header check - Ruby - Ruby Issue Tracking System
June 15, 2015 - That server encodes the content by Deflate. Deflate has not header. (see RFC 1951) Encoded content is not satisfy Gzib header and Zlib header, so Zlib::Inflate can not inflate it.
Find elsewhere
🌐
Google Groups
groups.google.com › g › comp.compression › c › XYxuFJutBts
"incorrect header check" using ZLIB Compression
January 3, 2020 - RWBoolean ZLIBCompressionImpl::compress( const char* data_in, unsigned int data_in_len, char* data_out, unsigned int* data_out_len ) { int err = ::compress( (unsigned char *) data_out, (unsigned long* ) data_out_len, (unsigned char *) data_in, (unsigned long) data_in_len ); if( err != Z_OK ) { THROW_SEP( "ZLIB compression error" ); }
🌐
GitHub
github.com › nodejs › node › issues › 3026
zlib.gunzip throws invalid headers check but createGunzip works · Issue #3026 · nodejs/node
September 23, 2015 - Hello, I've stumbled on a very strange problem. I am able to decompress gzipped file with zlib.createGunzip(), but zlib.gunzip() throws { [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }' In order to reproduce run the co...
Author   nodejs
🌐
GitHub
github.com › expressjs › body-parser › issues › 292
`deflate` encoded request results in `incorrect header check at Zlib.zlibOnError` · Issue #292 · expressjs/body-parser
March 12, 2018 - Doing the fetch seen here to the route seen here results in the following error. Error: incorrect header check at Zlib.zlibOnError [as onerror] (zlib.js:142:17) Am I doing something wrong? Perhaps the problem is with differences in how p...
Author   expressjs
🌐
GitHub
github.com › apigee › trireme › issues › 181
incorrect header check when zlib decompression in http · Issue #181 · apigee/trireme
March 16, 2018 - incorrect header check when zlib decompression in http#181 · Copy link · AdamMagaluk · opened · on Mar 16, 2018 · Issue body actions · 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.
Author   apigee
🌐
DNMTechs
dnmtechs.com › handling-zlib-error-in-python-3-incorrect-header-check
Handling zlib.error in Python 3: Incorrect Header Check – DNMTechs – Sharing and Storing Technology Knowledge
In the code snippet above, we use a try-except block to catch the zlib.error. We then check if the error message contains “Incorrect header check” using the in keyword. If it does, we print a specific error message indicating that the compressed data may be corrupted or in the wrong format.
🌐
GitHub
github.com › visionmedia › superagent › issues › 927
'incorrect header check' parsing gzip response on every call but the initial one · Issue #927 · forwardemail/superagent
March 8, 2016 - { [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR', response: null } Error: incorrect header check at Zlib._handle.onerror (zlib.js:363:17) From previous event: .... The Server resource headers returned (so it supports gzip).
Author   forwardemail
🌐
Ruby-Forum
ruby-forum.com › t › zlib-decompression-throws-header-error › 216620
Zlib decompression throws header error - Ruby - Ruby-Forum
February 9, 2012 - When I try to decompress the output data, it throws me the following error “Zlib::DataError: incorrect header check”. It might be very close to this issue - http://groups.google.com/group/nodejs/browse_thread/thread/32b9ee7f691a68d9 Here I attached my code snippets for your reference: require ...
🌐
Airbyte
discuss.airbyte.io › connector questions & issues
Zlib.error: Error -3 while decompressing data: incorrect header check - Connector Questions & Issues - Airbyte
June 22, 2023 - Is this your first time deploying Airbyte?: No OS Version / Instance: Ubuntu Memory / Disk: 32Gb / 120gb Deployment: Docker Swarm Airbyte Version: 0.42.1 Source name/version: Custom Amazon Seller Partner on v0.2.29 Destination name/version: MYSQL 0.1.20 Step: The issue is happening during sync Description: A few months ago i made a Custom Amazon Seller Partner connector on v0.2.29 (to fix the non renewal of the security token after 1 hour and because on 0.2.30 “GET_FLAT_FILE_ALL_ORDERS_DATA_...