all Python 3zlibterm
zlib
module
Compresses and decompresses data with the gzip-compatible DEFLATE algorithm, plus crc32 and adler32 checksums.
Syntax
import zlib
zlib works on bytes in memory: compressing a response, storing data compactly, computing a checksum. For .gz files use gzip — it adds a header that zlib.decompress() does not understand by default; for multi-file archives use zipfile. Only bytes can be compressed: encode a string first, for example with s.encode(), or you get TypeError.
Examples
import zlib
data = b"abc" * 1000
packed = zlib.compress(data)
print(len(data), len(packed) < 50) # → 3000 True
print(zlib.decompress(packed) == data) # → True
print(zlib.crc32(b"hello")) # → 907060870
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy