@@ -580,43 +580,21 @@ def _rewind(self):
580
580
self ._new_member = True
581
581
582
582
583
- def _create_simple_gzip_header (compresslevel : int ,
584
- mtime = None ) -> bytes :
585
- """
586
- Write a simple gzip header with no extra fields.
587
- :param compresslevel: Compresslevel used to determine the xfl bytes.
588
- :param mtime: The mtime (must support conversion to a 32-bit integer).
589
- :return: A bytes object representing the gzip header.
590
- """
591
- if mtime is None :
592
- mtime = time .time ()
593
- if compresslevel == _COMPRESS_LEVEL_BEST :
594
- xfl = 2
595
- elif compresslevel == _COMPRESS_LEVEL_FAST :
596
- xfl = 4
597
- else :
598
- xfl = 0
599
- # Pack ID1 and ID2 magic bytes, method (8=deflate), header flags (no extra
600
- # fields added to header), mtime, xfl and os (255 for unknown OS).
601
- return struct .pack ("<BBBBLBB" , 0x1f , 0x8b , 8 , 0 , int (mtime ), xfl , 255 )
602
-
603
-
604
583
def compress (data , compresslevel = _COMPRESS_LEVEL_BEST , * , mtime = None ):
605
584
"""Compress data in one shot and return the compressed string.
606
585
607
586
compresslevel sets the compression level in range of 0-9.
608
587
mtime can be used to set the modification time. The modification time is
609
588
set to the current time by default.
610
589
"""
611
- if mtime == 0 :
612
- # Use zlib as it creates the header with 0 mtime by default.
613
- # This is faster and with less overhead.
614
- return zlib .compress (data , level = compresslevel , wbits = 31 )
615
- header = _create_simple_gzip_header (compresslevel , mtime )
616
- trailer = struct .pack ("<LL" , zlib .crc32 (data ), (len (data ) & 0xffffffff ))
617
- # Wbits=-15 creates a raw deflate block.
618
- return (header + zlib .compress (data , level = compresslevel , wbits = - 15 ) +
619
- trailer )
590
+ # Wbits=31 automatically includes a gzip header and trailer.
591
+ gzip_data = zlib .compress (data , level = compresslevel , wbits = 31 )
592
+ if mtime is None :
593
+ mtime = time .time ()
594
+ # Reuse gzip header created by zlib, replace mtime and OS byte for
595
+ # consistency.
596
+ header = struct .pack ("<4sLBB" , gzip_data , int (mtime ), gzip_data [8 ], 255 )
597
+ return header + gzip_data [10 :]
620
598
621
599
622
600
def decompress (data ):
0 commit comments