On this page
zlib
Prev: zipimport | Up: stdlib_module | Next: zoneinfo
Rel: pickle, copyreg, shelve, marshal, dbm
Ref: docs.python.org — zlib
Tags: #public #python3 #stdlib
Import
import zlib
Overview
Low-level bindings to the C zlib DEFLATE compression library (compress, decompress, compressobj, decompressobj, crc32, adler32). Reach for zlib for raw in-memory or streaming DEFLATE payloads (HTTP Content-Encoding, Git loose objects, PNG chunks); use gzip when you need standard .gz files.
Quick Start
import zlib
raw = b"compress this repeated payload " * 50
compressed = zlib.compress(raw, level=6)
assert len(compressed) < len(raw)
assert zlib.decompress(compressed) == raw
assert zlib.crc32(raw) & 0xFFFFFFFF >= 0
Implementation Walkthrough
1. Bounded Streaming Decompression (decompressobj + max_length)
Always pass max_length= when decompressing untrusted data so a tiny 10 KB zip bomb cannot expand into gigabytes of RAM:
import zlib
def safe_decompress(data: bytes, *, max_bytes: int = 1_000_000) -> bytes:
decomp = zlib.decompressobj()
out = decomp.decompress(data, max_bytes + 1)
if len(out) > max_bytes or decomp.unconsumed_tail:
raise ValueError("Decompressed output exceeded max_bytes")
out += decomp.flush()
if len(out) > max_bytes:
raise ValueError("Decompressed output exceeded max_bytes")
return out
blob = zlib.compress(b"hello" * 10)
assert safe_decompress(blob, max_bytes=100) == b"hello" * 10
2. Raw DEFLATE vs. Gzip Framing (wbits)
wbits=15 (default) uses zlib headers; wbits=-15 is raw DEFLATE (no header); wbits=16 + 15 handles gzip framing.
Gotchas & Boundaries
- Never call
zlib.decompress(untrusted_bytes)without a size cap: Usedecompressobj().decompress(chunk, max_length)to prevent memory exhaustion. - Don't confuse
zlibwithgzip:zlib.compress()emits a 2-byte zlib header, not a.gzfile header; use gzip for.gzfiles. zlib.crc32()is an error-detection checksum, NOT a cryptographic hash: Use hashlib (sha256) for integrity against tampering.
Common Functions
- zlib.adler32() —
adler32(data, value=1, /)— Compute an Adler-32 checksum of data. - zlib.compress() —
compress(data, /, level=-1, wbits=15)— Returns a bytes object containing compressed data. - zlib.compressobj() —
compressobj(...)— Return a compressor object. - zlib.crc32() —
crc32(data, value=0, /)— Compute a CRC-32 checksum of data. - zlib.decompress() —
decompress(data, /, wbits=15, bufsize=16384)— Returns a bytes object containing the uncompressed data. - zlib.decompressobj() —
decompressobj(wbits=15, zdict=b'')— Return a decompressor object.
Classes & Objects
- zlib.error —
zlib.errorclass. (Methods:.add_note(),.with_traceback())
Constants & Attributes
zlib.DEFLATED(int) =8zlib.DEF_BUF_SIZE(int) =16384zlib.DEF_MEM_LEVEL(int) =8zlib.MAX_WBITS(int) =15zlib.ZLIB_RUNTIME_VERSION(str) ='1.2.12'zlib.ZLIB_VERSION(str) ='1.2.12'zlib.Z_BEST_COMPRESSION(int) =9zlib.Z_BEST_SPEED(int) =1zlib.Z_BLOCK(int) =5zlib.Z_DEFAULT_COMPRESSION(int) =-1zlib.Z_DEFAULT_STRATEGY(int) =0zlib.Z_FILTERED(int) =1zlib.Z_FINISH(int) =4zlib.Z_FIXED(int) =4zlib.Z_FULL_FLUSH(int) =3
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/zipimport
- tuts/python3/stdlib_module/zlib/adler32
- tuts/python3/stdlib_module/zlib/compress
- tuts/python3/stdlib_module/zlib/compressobj
- tuts/python3/stdlib_module/zlib/crc32
- tuts/python3/stdlib_module/zlib/decompress
- tuts/python3/stdlib_module/zlib/decompressobj
- tuts/python3/stdlib_module/zlib/error
- tuts/python3/stdlib_module/zoneinfo
Related notes
- tuts/python3/stdlib_module/gzip Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipimport Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/adler32 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/compress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/compressobj Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/crc32 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/decompress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/decompressobj Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib/error Linked here; links to this page; shared tags: #stdlib