After you decompress, you have binary data, you need to interpret it as an array of unsigned 32-bit integers, e.g. using frombuffer() from numpy. Answer from eishiya on discourse.mapeditor.org
🌐
GeeksforGeeks
geeksforgeeks.org › python › zlib-decompresss-in-python
zlib.decompress(s) in Python - GeeksforGeeks
March 6, 2020 - # using zlib.compress(s) method t = zlib.compress(s) print("Compressed String") print(t) print("\nDecompressed String") print(zlib.decompress(t)) Output : Compressed String b'x\x9c\x0b\xc9\xc8,V\x00"w7w\x85\xc4\xd2\x92\x8c\xfc"\x1d\x85\xc4\xbc\x14\x85\xb4\xcc\xbc\xc4\x1c\x85\xca\xd4\xc4"\x85\xe2\x92\xd2\x94\xd4\xbc\x12=\x00A\xc9\x0f\x0b' Decompressed String b'This is GFG author, and final year student.' Example #2 :
🌐
Stack Abuse
stackabuse.com › python-zlib-library-tutorial
Python zlib Library Tutorial
July 17, 2023 - This example is very similar to the previous one in that we're decompressing data that originates from a file, except that in this case we're going back to using the one-off decompress method, which decompresses the data in a single method call. This is useful for when your data is small enough to easily fit in memory. ... import zlib compressed_data = open('compressed.dat', 'rb').read() decompressed_data = zlib.decompress(compressed_data) print(decompressed_data)
🌐
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 ...
🌐
GitHub
github.com › oliverbaileysmith › zlib-decompress
GitHub - oliverbaileysmith/zlib-decompress: Python implementation of ZLIB decompression · GitHub
A Python implementation of ... and returns the decompressed data. Running py main.py shows an example of compressing input data using zlib.compress and decompressing using decompress....
Author   oliverbaileysmith
🌐
Delft Stack
delftstack.com › home › howto › python › python zlib
How to Compress and Decompress Data Using Zlib in Python | Delft Stack
February 2, 2024 - The following code snippet demonstrates how to decompress the previously compressed data with Python’s zlib.decompress() function.
🌐
Tiled Forum
discourse.mapeditor.org › questions
How to decompress base64 zlib data in Python? - Questions - Tiled Forum
March 16, 2023 - I have tile map graphical data compressed with <encoding=“base64” compression=“zlib”>. And in python, I’m trying to decompress that to get back the original data as a readable string or an array of nums. The compressed …
🌐
MicroPython
docs.micropython.org › en › latest › library › zlib.html
zlib – zlib compression & decompression — MicroPython latest documentation
The output formats are “raw” ... set the base-2 logarithm of the DEFLATE dictionary window size. So for example, wbits=10, wbits=-10, and wbits=26 all set the window size to 1024 bytes....
🌐
W3Schools
w3schools.com › python › ref_module_zlib.asp
Python zlib Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import zlib text = b'Hello Tobias! This is a message to compress.' compressed = zlib.compress(text) decompressed = zlib.decompress(compressed) print(f'Original size: {len(text)} bytes') print(f'Compressed size: {len(compressed)} bytes') print(f'Decompressed: {decompressed.decode()}') Try it Yourself »
🌐
w3resource
w3resource.com › python-exercises › extended-data-types › python-extended-data-types-bytes-bytearrays-exercise-8.php
Python Program: Compress and decompress bytes using zlib
August 11, 2025 - In the "main()" function, a sample ... decompressed results are printed. ... Write a Python program to compress a given bytes sequence using zlib and then decompress it back to the original bytes....
Find elsewhere
🌐
Bomberbot
bomberbot.com › python › zlib-decompression-in-python-unlocking-compressed-data
Zlib Decompression in Python: Unlocking Compressed Data - Bomberbot
July 18, 2025 - Let's explore some advanced techniques that will prove invaluable in your Python projects. When dealing with substantial compressed files or network streams, loading the entire dataset into memory isn't always feasible. For such cases, we can leverage zlib.decompressobj() to create a decompression object that processes data incrementally. This approach is particularly useful for handling large files or real-time data streams. Here's an example demonstrating incremental decompression:
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

🌐
Pyokagan
pyokagan.name › blog › 2019-10-18-zlibinflate
Let's implement zlib.decompress()
October 18, 2019 - Bits 5: FDICT --- If set, a DICT dictionary identifier is present immediately after the FLG byte. The dictionary is a sequence of bytes which is known beforehand to both the compressor and decompressor that can be used to achieve greater compression ratios. The zlib spec does not define any preset dictionaries and leaves it up to the implementor.
🌐
GitHub
github.com › c4collins › python-standard-library-examples › blob › master › 8.1-zlib.py
python-standard-library-examples/8.1-zlib.py at master · c4collins/python-standard-library-examples
compressed_data = zlib.compress(original_data) · print 'Compressed :', len(compressed_data), binascii.hexlify(compressed_data) · · decompressed_data = zlib.decompress(compressed_data) · print 'Decompressed:', len(decompressed_data), decompressed_data ·
Author   c4collins
🌐
GeeksforGeeks
geeksforgeeks.org › zlib-compresss-in-python
zlib.compress(s) in python - GeeksforGeeks
March 6, 2020 - With the help of zlib.decompress(s) method, we can decompress the compressed bytes of string into original string by using zlib.decompress(s) method. Syntax : zlib.decompress(string) Return : Return decompressed string. Example #1 : In this example we can see that by using zlib.decompress(s) method, 1 min read · bz2.compress(s) in Python ·
🌐
Python Module of the Week
pymotw.com › 2 › zlib
zlib – Low-level access to GNU zlib compression library - Python Module of the Week
It has some artificial chunking in place to illustrate the buffering behavior that happens when the data passed to compress() or decompress() doesn’t result in a complete block of compressed or uncompressed output. ... This server has obvious security implications. Do not run it on a system on the open internet or in any environment where security might be an issue. import zlib import logging import SocketServer import binascii BLOCK_SIZE = 64 class ZlibRequestHandler(SocketServer.BaseRequestHandler): logger = logging.getLogger('Server') def handle(self): compressor = zlib.compressobj(1) # F
🌐
O'Reilly
oreilly.com › library › view › python-standard-library › 0596000960 › ch02s23.html
The zlib Module - Python Standard Library [Book]
May 10, 2001 - File: zlib-example-1.py import zlib MESSAGE = "life of brian" compressed_message = zlib.compress(MESSAGE) decompressed_message = zlib.decompress(compressed_message) print "original:", repr(MESSAGE) print "compressed message:", repr(compressed_message) print "decompressed message:", repr(decompressed_message) original: 'life of brian' compressed message: 'x\234\313\311LKU\310OSH*\312L\314\003\000!\010\004\302' decompressed message: 'life of brian'
Author   Fredrik Lundh
Published   2001
Pages   304
🌐
Pycom
docs.pycom.io › tutorials › advanced › uzlib
Compressed files
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n' b = bytes(s, "utf-8") z = zlib.compress(b) with open('test.txt.z', 'wb') as f: f.write(z) print('ratio', len(z)/len(s)) This will create a file called test.txt.z with some content. Paste this file in your project and adjust the Pymakr settings to allow the uploading of this file type (Pymakr Settings –> Global settings –> Upload file types, add .z in the list). On your device, you can use the following example to decompress the file and print its contents:
🌐
Glitchdata
wiki.glitchdata.com › index.php › Python:_File_Compression_and_Decompression
Python: File Compression and Decompression - Glitchdata
December 29, 2022 - CHUNKSIZE=1024 d = zlib.decompressobj(16+zlib.MAX_WBITS) f = open(filename, 'rb') buffer=f.read(CHUNKSIZE) while buffer: outstr = d.decompress(buffer) print(outstr) buffer=f.read(CHUNKSIZE) outstr = d.flush() print(outstr) f.close() popen starts an OS process within python that will execute the given command string.