To decompress a gzip format file with zlib, call inflateInit2 with the windowBits parameter as 16+MAX_WBITS, like this:

inflateInit2(&stream, 16+MAX_WBITS);

If you don't do this, zlib will complain about a bad stream format. By default, zlib creates streams with a zlib header, and on inflate does not recognise the different gzip header unless you tell it so. Although this is documented starting in version 1.2.1 of the zlib.h header file, it is not in the zlib manual. From the header file:

windowBits can also be greater than 15 for optional gzip decoding. Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (the zlib format will return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a crc32 instead of an adler32.

Answer from Greg Hewgill on Stack Overflow
🌐
Python
docs.python.org › 3 › library › zlib.html
zlib — Compression compatible with gzip
+40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as the window size logarithm, and automatically accepts either the zlib or gzip format. When decompressing a stream, the window size must not be smaller than the size originally used to compress the stream; using a too-small value may result in an error exception.
Top answer
1 of 3
133

To decompress a gzip format file with zlib, call inflateInit2 with the windowBits parameter as 16+MAX_WBITS, like this:

inflateInit2(&stream, 16+MAX_WBITS);

If you don't do this, zlib will complain about a bad stream format. By default, zlib creates streams with a zlib header, and on inflate does not recognise the different gzip header unless you tell it so. Although this is documented starting in version 1.2.1 of the zlib.h header file, it is not in the zlib manual. From the header file:

windowBits can also be greater than 15 for optional gzip decoding. Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format (the zlib format will return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a crc32 instead of an adler32.

2 of 3
129

python

zlib library supports:

  • RFC 1950 (zlib compressed format)
  • RFC 1951 (deflate compressed format)
  • RFC 1952 (gzip compressed format)

The python zlib module will support these as well.

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)
>>> 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

For gzip data with gzip header you can 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()
🌐
Windrealm
windrealm.org › tutorials › decompress-gzip-stream.php
Decompressing a GZip Stream with Zlib
You may download the source code and Code::Blocks project files here. ... Here is the source code listing for the program. /* This C++ program uses zlib to read then decompress a gzipped file in memory.
🌐
GitHub
github.com › pfalcon › uzlib
GitHub - pfalcon/uzlib: Radically unbloated DEFLATE/zlib/gzip compression/decompression library. Can decompress any gzip/zlib data, and offers simplified compressor which produces gzip-compatible output, while requiring much less resources (and providing less compression ratio of course). · GitHub
Radically unbloated DEFLATE/zlib/gzip compression/decompression library. Can decompress any gzip/zlib data, and offers simplified compressor which produces gzip-compatible output, while requiring much less resources (and providing less compression ratio of course). - pfalcon/uzlib
Starred by 349 users
Forked by 95 users
Languages   C 83.7% | Assembly 12.2% | Makefile 1.8% | WebAssembly 1.2% | Shell 1.1%
🌐
Hackage
hackage.haskell.org › package › zlib
zlib: Compression and decompression in the gzip and zlib formats
This package provides a pure interface for compressing and decompressing streams of data represented as lazy ByteStrings. It uses the zlib C library so it has high performance. It supports the zlib, gzip and raw compression formats.
🌐
TechOverflow
techoverflow.net › 2020 › 01 › 10 › how-to-decompress-gzip-files-using-zlib-a-minimal-example
How to decompress gzip files using zlib - a minimal example | TechOverflow
March 30, 2026 - zlib is a great library in use by hundreds of thousands of programs (including Python). However, it lacks documentation. This minimal example shows you how to create a simple zcat-like program that decompresses a gzipped input file and prints its contents to stdout. zzcat.c · Copy Download ·
🌐
Code Beautify
codebeautify.org › zlib-decompress-online
Zlib Decompress Online to Zlib Decode Text
GZip Decompress Online · ZLib Decompress Online · Message · Add to Fav New Save & Share · Sample · Size : 0 , 0 Characters · Auto Zlib Decompress File.. Load URL · Size : 0 , 0 Characters · Copy To Clipboard Download · Ad blocking? It's okay. Please share on social media to support us: Tweet Share Post ·
🌐
Weblaro
weblaro.com › home › tools › gzip/zlib decompress online
Gzip/ZLib Decompress Online | Weblaro Tools
Decompress Gzip (.gz) or ZLib compressed data instantly with our free online tool. Just paste your compressed string to view the original uncompressed
Find elsewhere
🌐
Node.js
nodejs.org › api › zlib.html
Zlib | Node.js v26.4.0 Documentation
Compress data using deflate, and do not append a zlib header. ... Decompress a gzip stream.
🌐
zlib
zlib.net
zlib Home Site
All compression formats are permitted, since no decompression or recompression is performed. Encrypted entries are permitted, and pass through unscathed. zlib for Linux, both shared and static plus headers (RPM format, many architectures) ... DotNetZip zip file manipulation for .NET, and more (including replacements for the buggy Microsoft GZipStream ...
🌐
99Tools
99tools.net › home › compress decompress tools › zlib decompress online
ZLib Decompress Online - 99Tools
January 11, 2026 - ZLib Decompress · Decompressed Output: Copy to Clipboard Download .txt · Related Tools · Gzip Decompress Online · Share This Tool · Paste your ZLib compressed string in the input box · Click ZLib Decompress · View the decompressed output instantly · Copy or download the result if needed ·
🌐
zlib
zlib.net › manual.html
zlib 1.3.1 Manual
If a zlib stream is being decoded, then head->done is set to –1 to indicate that there will be no gzip header information forthcoming. Note that Z_BLOCK can be used to force inflate() to return immediately after header processing is complete and before any actual data is decompressed.
🌐
GitHub
github.com › merainc › gzip_cpp
GitHub - merainc/gzip_cpp: A C++ library to compress/decompress data by using zlib. · GitHub
This is a c++ library which will be able to compress/decompress your data buffers. It is a bunch of wrap classes of C library zlib. You need install zlib developer files to your system before you compile library.
Starred by 17 users
Forked by 9 users
Languages   C++ 85.4% | Python 11.5% | CMake 0.8% | Shell 0.7% | Makefile 0.6% | M4 0.6% | C 0.4%
🌐
GitHub
github.com › ebiggers › libdeflate
GitHub - ebiggers/libdeflate: Heavily optimized library for DEFLATE/zlib/gzip compression and decompression · GitHub
Heavily optimized library for DEFLATE/zlib/gzip compression and decompression - ebiggers/libdeflate
Starred by 1.3K users
Forked by 211 users
Languages   C 90.3% | Shell 5.3% | CMake 3.1% | Python 1.3%
🌐
Stack Abuse
stackabuse.com › python-zlib-library-tutorial
Python zlib Library Tutorial
July 17, 2023 - Compression and decompression can both be done as a one-off operation, or by splitting the data into chunks like you'd seen from a stream of data. Both modes of operation are explained in this article. One of the best things, in my opinion, about the zlib library is that it is compatible with the gzip ...
🌐
Ruby-Doc.org
ruby-doc.org › stdlib-2.6.3 › libdoc › zlib › rdoc › Zlib.html
Module: Zlib (Ruby 2.6.3)
The zlib compression library provides ... and decompression functions, including integrity checks of the uncompressed data. The zlib compressed data format is described in RFC 1950, which is a wrapper around a deflate stream which is described in RFC 1951. The library also supports reading and writing files in gzip (.gz) format ...
🌐
DEV Community
dev.to › biellls › compression-clearing-the-confusion-on-zip-gzip-zlib-and-deflate-15g1
Compression: Clearing the Confusion on ZIP, GZIP, Zlib and DEFLATE - DEV Community
January 21, 2022 - We also noticed that even though Snowflake was unable to detect the zlib compressed file, if we told it that it's a GZIPped file it was able to load it without issues. Clearly the claim of compatibility wasn't completely outlandish. After a few weeks I decided to dive in and investigate in depth. The following is a summary of what I learned. I was surprised to find out that GZIP, zlib or even ZIP are not compression algorithms, they are actually file formats that can permit different compression algorithms.
🌐
Openmv
docs.openmv.io › openmv micropython › openmv micropython libraries › zlib — zlib compression & decompression
zlib — zlib compression & decompression - OpenMV MicroPython 1.28 documentation
This module allows compression and decompression of binary data with the DEFLATE algorithm (commonly used in the zlib library and gzip archiver).
🌐
Ubuntu
manpages.ubuntu.com › focal › man(3)
Ubuntu Manpage: zlib - compression and decompression operations
The stream will be a compressing stream that produces raw output, using compression level level (if specified) which will be an integer from 0 to 9, and the compression dictionary bindata (if specified). Note that the raw compressed data includes no metadata about what compression dictionary was used, if any; that is a feature of the zlib-format data. ... The stream will be a decompressing stream that takes gzip-format input and produces uncompressed output.