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
🌐
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...
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
Zlib uncompression for an idiot? | MajorGeeks.Com Support Forums
I want to uncompress a ZLIB-compressed file, without any programming skill. Could some of you give me exact help? More on forums.majorgeeks.com
🌐 forums.majorgeeks.com
September 25, 2010
compression - zlib decompression in python - Stack Overflow
Okay so I have some data streams compressed by python's (2.6) zlib.compress() function. When I try to decompress them, some of them won't decompress (zlib error -5, which seems to be a "buffer erro... More on stackoverflow.com
🌐 stackoverflow.com
c++ - How can I easily compress and decompress files using zlib? - Stack Overflow
I don't find the documentation very easy. Isn't there just a simple function like compress() decompress()? 2011-04-13T12:29:34.647Z+00:00 ... @Downvoter: Care to explain why the downvote? The OP never mentioned in the Orginal Question that he/she did not understand the examples on zlib website. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Code Beautify
codebeautify.org › zlib-decompress-online
Zlib Decompress Online to Zlib Decode Text
This tool helps you to decompress your zlib data to plain Text with Ease.
🌐
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.
🌐
GitHub
github.com › kevin-cantwell › zlib
GitHub - kevin-cantwell/zlib: A command-line utility for quickly compressing or decompressing zlib data. · GitHub
Without any arguments, zlib will compress an input stream. Use the -d flag for decompression.
Starred by 48 users
Forked by 4 users
Languages   Go
Find elsewhere
🌐
Jsontotable
jsontotable.org › zlib-decompression
Free Online ZLIB Decompressor - Decompress & Extract .zlib Files | Inflate Tool | JSON to Table Converter
Free online ZLIB decompression tool. Decompress .zlib files instantly. Extract compressed JSON, XML, text data. Inflate ZLIB compressed data online free. Upload files or paste base64. No registration. 100% secure browser-based tool.
🌐
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?
🌐
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).
Top answer
1 of 4
36

According to RFC 1950 , the difference between the "OK" 0x789C and the "bad" 0x78DA is in the FLEVEL bit-field:

  FLEVEL (Compression level)
     These flags are available for use by specific compression
     methods.  The "deflate" method (CM = 8) sets these flags as
     follows:

        0 - compressor used fastest algorithm
        1 - compressor used fast algorithm
        2 - compressor used default algorithm
        3 - compressor used maximum compression, slowest algorithm

     The information in FLEVEL is not needed for decompression; it
     is there to indicate if recompression might be worthwhile.

"OK" uses 2, "bad" uses 3. So that difference in itself is not a problem.

To get any further, you might consider supplying the following information for each of compressing and (attempted) decompressing: what platform, what version of Python, what version of the zlib library, what was the actual code used to call the zlib module. Also supply the full traceback and error message from the failing decompression attempts. Have you tried to decompress the failing files with any other zlib-reading software? With what results? Please clarify what you have to work with: Does "Am I hosed?" mean that you don't have access to the original data? How did it get from a stream to a file? What guarantee do you have that the data was not mangled in transmission?

UPDATE Some observations based on partial clarifications published in your self-answer:

You are using Windows. Windows distinguishes between binary mode and text mode when reading and writing files. When reading in text mode, Python 2.x changes '\r\n' to '\n', and changes '\n' to '\r\n' when writing. This is not a good idea when dealing with non-text data. Worse, when reading in text mode, '\x1a' aka Ctrl-Z is treated as end-of-file.

To compress a file:

# imports and other superstructure left as a exercise
str_object1 = open('my_log_file', 'rb').read()
str_object2 = zlib.compress(str_object1, 9)
f = open('compressed_file', 'wb')
f.write(str_object2)
f.close()

To decompress a file:

str_object1 = open('compressed_file', 'rb').read()
str_object2 = zlib.decompress(str_object1)
f = open('my_recovered_log_file', 'wb')
f.write(str_object2)
f.close()

Aside: Better to use the gzip module which saves you having to think about nasssties like text mode, at the cost of a few bytes for the extra header info.

If you have been using 'rb' and 'wb' in your compression code but not in your decompression code [unlikely?], you are not hosed, you just need to flesh out the above decompression code and go for it.

Note carefully the use of "may", "should", etc in the following untested ideas.

If you have not been using 'rb' and 'wb' in your compression code, the probability that you have hosed yourself is rather high.

If there were any instances of '\x1a' in your original file, any data after the first such is lost -- but in that case it shouldn't fail on decompression (IOW this scenario doesn't match your symptoms).

If a Ctrl-Z was generated by zlib itself, this should cause an early EOF upon attempted decompression, which should of course cause an exception. In this case you may be able to gingerly reverse the process by reading the compressed file in binary mode and then substitute '\r\n' with '\n' [i.e. simulate text mode without the Ctrl-Z -> EOF gimmick]. Decompress the result. Edit Write the result out in TEXT mode. End edit

UPDATE 2 I can reproduce your symptoms -- with ANY level 1 to 9 -- with the following script:

import zlib, sys
fn = sys.argv[1]
level = int(sys.argv[2])
s1 = open(fn).read() # TEXT mode
s2 = zlib.compress(s1, level)
f = open(fn + '-ct', 'w') # TEXT mode
f.write(s2)
f.close()
# try to decompress in text mode
s1 = open(fn + '-ct').read() # TEXT mode
s2 = zlib.decompress(s1) # error -5
f = open(fn + '-dtt', 'w')
f.write(s2)
f.close()

Note: you will need a use a reasonably large text file (I used an 80kb source file) to ensure that the decompression result will contain a '\x1a'.

I can recover with this script:

import zlib, sys
fn = sys.argv[1]
# (1) reverse the text-mode write
# can't use text-mode read as it will stop at Ctrl-Z
s1 = open(fn, 'rb').read() # BINARY mode
s1 = s1.replace('\r\n', '\n')
# (2) reverse the compression
s2 = zlib.decompress(s1)
# (3) reverse the text mode read
f = open(fn + '-fixed', 'w') # TEXT mode
f.write(s2)
f.close()

NOTE: If there is a '\x1a' aka Ctrl-Z byte in the original file, and the file is read in text mode, that byte and all following bytes will NOT be included in the compressed file, and thus can NOT be recovered. For a text file (e.g. source code), this is no loss at all. For a binary file, you are most likely hosed.

Update 3 [following late revelation that there's an encryption/decryption layer involved in the problem]:

The "Error -5" message indicates that the data that you are trying to decompress has been mangled since it was compressed. If it's not caused by using text mode on the files, suspicion obviously(?) falls on your decryption and encryption wrappers. If you want help, you need to divulge the source of those wrappers. In fact what you should try to do is (like I did) put together a small script that reproduces the problem on more than one input file. Secondly (like I did) see whether you can reverse the process under what conditions. If you want help with the second stage, you need to divulge the problem-reproduction script.

2 of 4
4

I was looking for

python -c 'import sys,zlib;sys.stdout.write(zlib.decompress(sys.stdin.read()))'

wrote it myself; based on answers of zlib decompression in python

🌐
zlib
zlib.net
zlib Home Site
Jean-loup is also the primary author ... Zip; Mark is also the author of gzip's and UnZip's main decompression routines and was the original author of Zip. Not surprisingly, the compression algorithm used in zlib is essentially the same as that in gzip and Zip, namely, the `deflate' ...
🌐
GitHub
gist.github.com › arq5x › 5315739
Compress and then Decompress a string with zlib. · GitHub
// 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.
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › zlib-decompresss-in-python
zlib.decompress(s) in Python - GeeksforGeeks
March 6, 2020 - Syntax : zlib.decompress(string) Return : Return decompressed string.
🌐
OnlinePHP
onlinephp.io › zlib-decode
zlib_decode - Online Tool
zlib_decode · Execute zlib_decode with this online tool zlib_decode() - Uncompress any raw/gzip/zlib encoded data · Zlib Decode Online Tool · Manual · Code Examples · Zlib Decode Online Tool · Manual · Code Examples · hex2bin · json_decode ...
🌐
CRAN
cran.r-project.org › web › packages › zlib › zlib.pdf pdf
zlib: Compression and Decompression
October 18, 2023 - Initialize a new compressor object for zlib-based compression with specified settings. ... Compression level, integer between 0 and 9, or -1 for default. ... Compression method. ... Window size bits. ... Memory level for internal compression state. ... Compression strategy. ... Optional predefined compression dictionary as a raw vector. ... A SEXP pointer to the new compressor object. ... Initialize a new decompressor object for zlib-based decompression.
🌐
Pyokagan
pyokagan.name › blog › 2019-10-18-zlibinflate
Let's implement zlib.decompress()
October 18, 2019 - Bits 0 to 3: CM --- Compression Method. This identifies the compression method in the file. Only CM=8 is defined by the zlib spec, which basically says that the zlib file contains data compressed with DEFLATE.
🌐
Tiled Forum
discourse.mapeditor.org › questions
How to decompress zlib-compressed map data on tiled map files in C++? - Questions - Tiled Forum
May 8, 2023 - I know there has been already similar posts to this, but I couldn’t find any that works. I’ve used the exact same decompress function from this post. It didn’t seem to work and only returned “???”. The reason why I did…
🌐
Wikipedia
en.wikipedia.org › wiki › Zlib
zlib - Wikipedia
1 week ago - Some ancillary code (counters) may suffer from overflow for long data streams, but this does not affect the actual compression or decompression. When compressing a long (or infinite) data stream, it is advisable to write regular full-flush points. Today, zlib is something of a de facto standard, ...