diff options
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r-- | Lib/gzip.py | 38 |
1 files changed, 8 insertions, 30 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 0d19c84..ba753ce 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -580,27 +580,6 @@ class _GzipReader(_compression.DecompressReader): self._new_member = True -def _create_simple_gzip_header(compresslevel: int, - mtime = None) -> bytes: - """ - Write a simple gzip header with no extra fields. - :param compresslevel: Compresslevel used to determine the xfl bytes. - :param mtime: The mtime (must support conversion to a 32-bit integer). - :return: A bytes object representing the gzip header. - """ - if mtime is None: - mtime = time.time() - if compresslevel == _COMPRESS_LEVEL_BEST: - xfl = 2 - elif compresslevel == _COMPRESS_LEVEL_FAST: - xfl = 4 - else: - xfl = 0 - # Pack ID1 and ID2 magic bytes, method (8=deflate), header flags (no extra - # fields added to header), mtime, xfl and os (255 for unknown OS). - return struct.pack("<BBBBLBB", 0x1f, 0x8b, 8, 0, int(mtime), xfl, 255) - - def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): """Compress data in one shot and return the compressed string. @@ -608,15 +587,14 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None): mtime can be used to set the modification time. The modification time is set to the current time by default. """ - if mtime == 0: - # Use zlib as it creates the header with 0 mtime by default. - # This is faster and with less overhead. - return zlib.compress(data, level=compresslevel, wbits=31) - header = _create_simple_gzip_header(compresslevel, mtime) - trailer = struct.pack("<LL", zlib.crc32(data), (len(data) & 0xffffffff)) - # Wbits=-15 creates a raw deflate block. - return (header + zlib.compress(data, level=compresslevel, wbits=-15) + - trailer) + # Wbits=31 automatically includes a gzip header and trailer. + gzip_data = zlib.compress(data, level=compresslevel, wbits=31) + if mtime is None: + mtime = time.time() + # Reuse gzip header created by zlib, replace mtime and OS byte for + # consistency. + header = struct.pack("<4sLBB", gzip_data, int(mtime), gzip_data[8], 255) + return header + gzip_data[10:] def decompress(data): |