all Python 3zlibfunction
zlib.decompressobj()
compression
Creates a streaming decompressor: it takes compressed data in chunks and knows where the stream ended.
Syntax
d = zlib.decompressobj(wbits=15, zdict=b'')
The eof attribute turns True once the compressed stream is complete, and whatever arrived after it lands in unused_data — that is how you split several back-to-back streams or a stream with trailing bytes. The max_length argument of decompress() caps the output and keeps the rest in unconsumed_tail: protection against zip bombs, where a megabyte expands into gigabytes.
Examples
import zlib
packed = zlib.compress(b'abc' * 100)
d = zlib.decompressobj()
out = d.decompress(packed[:5])
print(d.eof) # → False
out += d.decompress(packed[5:] + b'TAIL')
print(len(out), d.eof, d.unused_data) # → 300 True b'TAIL'
import zlib
bomb = zlib.compress(b'\0' * 10_000_000)
d = zlib.decompressobj()
chunk = d.decompress(bomb, 1024)
print(len(bomb) < 20_000, len(chunk)) # → True 1024 — выдача ограничена
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy