It is also possible to decompress it using standard shell-script + gzip, if you don't have, or want to use openssl or other tools.
The trick is to prepend the gzip magic number and compress method to the actual data from zlib.compress:

printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" |cat - /tmp/data |gzip -dc >/tmp/out

Edits:
@d0sboots commented: For RAW Deflate data, you need to add 2 more null bytes:
โ†’ "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00"

This Q on SO gives more information about this approach. An answer there suggests that there is also an 8 byte footer.

Users @Vitali-Kushner and @mark-bessey reported success even with truncated files, so a gzip footer does not seem strictly required.

@tobias-kienzler suggested this function for the bashrc:
zlibd() (printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - "$@" | gzip -dc)

Answer from wkpark on Stack Exchange
๐ŸŒ
Ubuntu
manpages.ubuntu.com โ€บ focal โ€บ man(3)
Ubuntu Manpage: zlib - compression and decompression operations
The transformation can be removed again with chan pop. The mode argument determines what type of transformation is pushed; the following are supported: ... The transformation will be a compressing transformation that produces zlib-format data on channel, which must be writable.
Discussions

Zlib uncompression for an noob

Looks like zlib compressed data, here's more info how to deflate it: https://unix.stackexchange.com/questions/22834/how-to-uncompress-zlib-data-in-unix

More on reddit.com
๐ŸŒ r/LiveOverflow
5
6
March 27, 2020
red lang - How to decompress/deflate zlib data [rfc1951]? - Stack Overflow
I am looking for decompressing data according to the deflate compression mechanism [rfc1951]. The linux command for this mechanism is: zlib-flate -uncompress I try the Red command decompress with More on stackoverflow.com
๐ŸŒ stackoverflow.com
blob - How can I DEFLATE with a command line tool to extract a Git object? - Stack Overflow
Related question for people who ... want to decompress data with cURL: stackoverflow.com/questions/8364640/โ€ฆ ... Save this answer. ... Show activity on this post. Something like the following will print the raw content, including the "$type $length\0" header: perl -MCompress::Zlib -e 'undef ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I decompress a gzip stream with zlib? - Stack Overflow
Gzip format files (created with the gzip program, for example) use the "deflate" compression algorithm, which is the same compression algorithm as what zlib uses. However, when using zlib to inflat... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ kevin-cantwell โ€บ zlib
GitHub - kevin-cantwell/zlib: A command-line utility for quickly compressing or decompressing zlib data. ยท GitHub
USAGE: zlib [global options] command [command options] [arguments...] VERSION: 0.0.0 COMMANDS: help, h Shows a list of commands or help for one command GLOBAL OPTIONS: -d, --decompress Decompresses the input instead of compressing the output.
Starred by 48 users
Forked by 4 users
Languages ย  Go
๐ŸŒ
SysTutorials
systutorials.com โ€บ docs โ€บ linux โ€บ man โ€บ n-zlib
zlib: compression and decompression operations - Linux Manuals (n)
Pushes a compressing or decompressing transformation onto the channel channel. The transformation can be removed again with chan pop. The mode argument determines what type of transformation is pushed; the following are supported: ... The ...
๐ŸŒ
Jyotiprakash's Blog
blog.jyotiprakash.org โ€บ file-compression-and-decompression-in-c-using-zlib
File compression and decompression in C using ZLib
December 31, 2023 - To create a C program that uses Zlib to compress or decompress a file based on command-line arguments, you need to follow these steps: Install Zlib for Development on Ubuntu: Open a terminal and run the following command to install Zlib development...
๐ŸŒ
Linux Man Pages
linux.die.net โ€บ man โ€บ 3 โ€บ zlib
zlib(3): compression/decompression library - Linux man page
The zlib library is a general purpose data compression library. The code is thread safe. It provides in-memory compression and decompression functions, including integrity checks of the uncompressed data.
๐ŸŒ
Linuxbase
refspecs.linuxbase.org โ€บ LSB_3.0.0 โ€บ LSB-Core-generic โ€บ LSB-Core-generic โ€บ zlib-uncompress-1.html
uncompress - Linux Foundation Referenced Specifications
int uncompress(Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen); ยท The uncompress() function shall attempt to uncompress sourceLen bytes of data in the buffer source, placing the result in the buffer dest
Find elsewhere
๐ŸŒ
Code Beautify
codebeautify.org โ€บ zlib-decompress-online
Zlib Decompress Online to Zlib Decode Text
Zlib to Decompress Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.
๐ŸŒ
SysTutorials
systutorials.com โ€บ docs โ€บ linux โ€บ man โ€บ 1-zlib_decompress
zlib_decompress: decompress mysqlpump ZLIB-compressed output - Linux Manuals (1)
The zlib_decompress utility decompresses mysqlpump output that was created using ZLIB compression. Note If MySQL was configured with the -DWITH_ZLIB=system option,
๐ŸŒ
YouTube
youtube.com โ€บ peter schneider
How to uncompress zlib data in UNIX? - YouTube
How to uncompress zlib data in UNIX?I hope you found a solution that worked for you :) The Content (except music & images) is licensed under (https://meta.st...
Published ย  December 6, 2022
Views ย  826
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69105129 โ€บ how-to-decompress-deflate-zlib-data-rfc1951
red lang - How to decompress/deflate zlib data [rfc1951]? - Stack Overflow
I am looking for decompressing data according to the deflate compression mechanism [rfc1951]. The linux command for this mechanism is: zlib-flate -uncompress I try the Red command decompress with
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()
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ zlib.html
zlib โ€” Compression compatible with gzip
For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. This is an optional module. If it is missing from your copy...
๐ŸŒ
zlib
zlib.net โ€บ zlib_how.html
zlib Usage Example
February 12, 2026 - /* decompress until deflate stream ends or end of file */ do { We read input data and set the strm structure accordingly. If we've reached the end of the input file, then we leave the outer loop and report an error, since the compressed data is incomplete. Note that we may read more data than is eventually consumed by inflate(), if the input file continues past the zlib stream.
๐ŸŒ
MajorGeeks
forums.majorgeeks.com โ€บ threads โ€บ zlib-uncompression-for-an-idiot.223640
Zlib uncompression for an idiot? | MajorGeeks.Com Support Forums
September 25, 2010 - I want to uncompress a ZLIB-compressed file, without any programming skill. Could some of you give me exact help?
๐ŸŒ
GitHub
gist.github.com โ€บ arq5x โ€บ 5315739
Compress and then Decompress a string with zlib. ยท GitHub - Gist
// deflate a into b. (that is, compress a into b) // zlib struct z_stream defstream; defstream.zalloc = Z_NULL; defstream.zfree = Z_NULL; defstream.opaque = Z_NULL; // setup "a" as the input and "b" as the compressed output defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator defstream.next_in = (Bytef *)a; // input char array defstream.avail_out = (uInt)sizeof(b); // size of output defstream.next_out = (Bytef *)b; // output char array // the actual compression work.
๐ŸŒ
Unix.com
unix.com โ€บ applications โ€บ programming
uncompress of zlib - Programming - Unix Linux Community
October 20, 2017 - When I gzopen & gzread from a gzip file, it works OK. But I when I try to uncompress the same data from memory (either by reading to memory with fread or mmap()ing) using decompress, I get Z_DATA_ERROR. Is it because gziโ€ฆ