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()
🌐
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 ...
Starred by 349 users
Forked by 95 users
Languages   C 83.7% | Assembly 12.2% | Makefile 1.8% | WebAssembly 1.2% | Shell 1.1%
🌐
Windrealm
windrealm.org › tutorials › decompress-gzip-stream.php
Decompressing a GZip Stream with Zlib
Author: Andrew Lim Chong Liang http://windrealm.org */ #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include "zlib.h" #include "zconf.h" using namespace std; bool gzipInflate( const std::string& compressedBytes, std::string& uncompressedBytes ) { if ( compressedBytes.size() == 0 ) { uncompressedBytes = compressedBytes ; return true ; } uncompressedBytes.clear() ; unsigned full_length = compressedBytes.size() ; unsigned half_length = compressedBytes.size() / 2; unsigned uncompLength = full_length ; char* uncomp = (char*) calloc( sizeof(char), uncompLength ); z_strea
🌐
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.
🌐
Node.js
nodejs.org › api › zlib.html
Zlib | Node.js v26.4.0 Documentation
The node:zlib module provides compression functionality implemented using Gzip, Deflate/Inflate, Brotli, and Zstd. ... Compression and decompression are built around the Node.js Streams API.
🌐
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 - Not copyrighted -- provided to the public domain */ #include <stdio.h> #include <zlib.h> #define BUFSIZE 16384 /* compress or decompress from fin (command line argument) to stdout */ int main(int argc, char **argv) { if(argc <= 1) { // <= (number of expected CLI arguments) fprintf(stderr, "Usage: %s <input file>\n", argv[0]); return -1; } gzFile fin = gzopen(argv[1], "rb"); char buf[BUFSIZE]; int n; while((n = gzread(fin, buf, BUFSIZE)) > 0) { fwrite(buf, 1, n, stdout); } return 0; } ... # Create test file echo "foo" | gzip -c > test.txt.gz # Uncompress using zzcat!
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › DecompressionStream › DecompressionStream
DecompressionStream: DecompressionStream() constructor - Web APIs | MDN
2 weeks ago - Decompress the stream using the GZIP algorithm. ... Decompress the stream using the DEFLATE algorithm in ZLIB Compressed Data Format.
Find elsewhere
🌐
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
🌐
Code Beautify
codebeautify.org › zlib-decompress-online
Zlib Decompress Online to Zlib Decode Text
zlib-decompress-online?url= https://gist.gi · thubusercontent · .com/cbmgit/6fb · 8e04861977b3567 · 530c5a3260e5f3/ raw/zlibdata.tx · t · Related Tools · Gzip Decompress Base64 to XML Base64 to Text Image to Base64 · Recently visited pages · Tags · Compress / Decompress ·
🌐
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%
🌐
Python
docs.python.org › 3 › library › gzip.html
gzip — Support for gzip files
This function is capable of decompressing multi-member gzip data (multiple gzip blocks concatenated together). When the data is certain to contain only one member the zlib.decompress() function with wbits set to 31 is faster.
🌐
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 in-memory compression 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.
🌐
Stack Abuse
stackabuse.com › python-zlib-library-tutorial
Python zlib Library Tutorial
July 17, 2023 - The file is then decompressed using chunks of data. Again, in this example the file doesn't contain a massive amount of data, but nevertheless, it serves the purpose of explaining the buffer concept. ... import zlib data = 'Hello world' compress = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, +15) compressed_data = compress.compress(data) compressed_data += compress.flush() print('Original: ' + data) print('Compressed data: ' + compressed_data) f = open('compressed.dat', 'w') f.write(compressed_data) f.close() CHUNKSIZE = 1024 data2 = zlib.decompressobj() my_file = open('compressed.dat', 'rb') buf = my_file.read(CHUNKSIZE) # Decompress stream chunks while buf: decompressed_data = data2.decompress(buf) buf = my_file.read(CHUNKSIZE) decompressed_data += data2.flush() print('Decompressed data: ' + decompressed_data) my_file.close()
🌐
Ubuntu
manpages.ubuntu.com › focal › man(3)
Ubuntu Manpage: zlib - compression and decompression operations
The transformation will be a compressing transformation that produces gzip-format data on channel, which must be writable. ... The transformation will be a decompressing transformation that reads raw compressed data from channel, which must be readable. The following options may be set when creating a transformation via the “options ...” to the zlib push command:
🌐
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).
🌐
Pycom
docs.pycom.io › tutorials › advanced › uzlib
Compressed files
On your device, you can run the following to decompress the file: import zlib import os #verify the presence of the file file = 'test.txt.gz' print('dir', os.listdir()) print(file, os.stat(file)) with open('test.txt.gz', 'r') as file: data = zlib.DecompIO(file, 31) #use data.readline() to read ...