Python Glossary Русский

all Python 3zlibfunction

zlib.decompress()

compression

Decompresses zlib-compressed bytes; the wbits parameter picks the format: zlib, gzip or raw DEFLATE.

Syntax

zlib.decompress(data, /, wbits=15, bufsize=16384)

With the default wbits=15 it only understands the zlib format: data from gzip.compress fails with 'incorrect header check'. Use wbits=31 for gzip, 47 to auto-detect zlib or gzip, and -15 for raw DEFLATE (as inside ZIP). A truncated stream raises zlib.error with code -5. The whole result is built in memory — for untrusted data (zip bombs) decompressobj() with max_length is safer.

Examples

import gzip, zlib
gz = gzip.compress(b'data')
try:
    zlib.decompress(gz)
except zlib.error as e:
    print(e)  # → Error -3 while decompressing data: incorrect header check
print(zlib.decompress(gz, wbits=31))  # → b'data'
print(zlib.decompress(gz, wbits=47))  # → b'data'
import zlib
packed = zlib.compress(b'abc' * 100)
try:
    zlib.decompress(packed[:-4])
except zlib.error as e:
    print(e)  # → Error -5 while decompressing data: incomplete or truncated stream

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy