Python 3.2+gzipfunction
gzip.compress()
in-memory compression
Compresses bytes into the gzip format entirely in memory; the result is readable by gzip.open and gunzip.
Syntax
gzip.compress(data, compresslevel=6, *, mtime=0)
The gzip header stores an mtime: before Python 3.14 it defaulted to the current time, so identical data compressed in different seconds gave different bytes — breaking hashes and reproducible builds. Since 3.14 the default is mtime=0; pass mtime=0 explicitly if your code must also run on older versions. Like zlib.compress, it accepts only bytes. Since 3.15 the default compression level is 6, not 9: compression is faster and the archive slightly larger; pass compresslevel=9 explicitly to keep the old size.
Examples
import gzip
a = gzip.compress(b'report', mtime=0)
b = gzip.compress(b'report', mtime=0)
print(a == b) # → True — mtime=0 даёт одинаковые байты
print(a[:2]) # → b'\x1f\x8b'
print(gzip.decompress(a)) # → b'report'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy