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...
๐ŸŒ
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
๐ŸŒ
Code Beautify
codebeautify.org โ€บ zlib-decompress-online
Zlib Decompress Online to Zlib Decode Text
This tool allows loading the Zlib data URL converting to plain string. Click on the URL button, Enter URL and Submit. This tool supports loading the Zlib data File to decompress to Text.
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

๐ŸŒ
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 - This C program is designed to either compress or decompress a file using the Zlib library. It takes three command-line arguments: the input file name, the operation (either "compress" or "decompress"), and the output file name.
๐ŸŒ
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()
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.
๐ŸŒ
zlib
zlib.net โ€บ manual.html
zlib 1.3.1 Manual
The data format used by the zlib ... ZLIB_VERSION "1.3.1" #define ZLIB_VERNUM 0x1310 ยท The zlib compression library provides in-memory compression and decompression functions, including integrity checks of the uncompressed data....
๐ŸŒ
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.
๐ŸŒ
Ubuntu Manpages
manpages.ubuntu.com โ€บ manpages โ€บ bionic โ€บ man3 โ€บ zlib.3tcl.html
Ubuntu Manpage: zlib - compression and decompression operations
This will be in the same as is returned by clock seconds or file mtime. type The type of the data being compressed, being binary or text. zlib inflate ... bufferSize is a hint as to what size of buffer is to be used to receive the data. CHANNEL SUBCOMMAND zlib push ... mode argument determines what type of transformation is pushed; the following are supported: compress The transformation will be a compressing transformation that produces zlib-format data on ยท channel, which must be writable. decompress The transformation will be a decompressing transformation that reads zlib-format data from
๐ŸŒ
STMicroelectronics Community
community.st.com โ€บ stmicroelectronics community โ€บ discussions โ€บ product forums โ€บ stm32 mcus software development tools โ€บ stm32cubeide (mcus) โ€บ decompressing a file from zlib library
decompressing a file from zlib library | Community
February 21, 2022 - I wasn't sure of what would be the extension of the compressed file so I've added ".z" at the end which the windows do recognise as a compressed file but 7zip cannot decompress. When using the inflate() function it returns Z_DATA_ERROR (-3) ... This topic has been closed for replies. ... Typically when building/validating this stuff you'd build PC side pack/unpack programs using ZLIB, prove you got all the logic right, perhaps test on the unpacked equivalent to your 3MB to 26KB case, and make sure it's not outlandish.
๐ŸŒ
Toolexe
toolexe.com โ€บ home โ€บ compression โ€บ zlib decompressor
zLib Decompress - Free Online Compression Tool - Toolexe
Decompress zlib files online instantly. Free zlib decompress tool supports text, Base64, Hex formats. No uploads required. Process files securely.
๐ŸŒ
SysTutorials
systutorials.com โ€บ docs โ€บ linux โ€บ man โ€บ n-zlib
zlib: compression and decompression operations - Linux Manuals (n)
Creates a streaming compression or decompression command based on the mode, and return the name of the command. For a description of how that command works, see STREAMING INSTANCE COMMAND below. The following modes and options are supported: zlib stream compress ?-dictionary bindata?
๐ŸŒ
CRAN
cran.r-project.org โ€บ web โ€บ packages โ€บ zlib โ€บ zlib.pdf pdf
zlib: Compression and Decompression
October 18, 2023 - file in chunks and tries to decompress it using the zlib library.
๐ŸŒ
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). ... Prefer to use deflate.DeflateIO instead of the functions in this module as it provides a streaming interface to compression and decompression ...
๐ŸŒ
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.