1010FTEXT , FHCRC , FEXTRA , FNAME , FCOMMENT = 1 , 2 , 4 , 8 , 16
1111
1212def write32 (output , value ):
13- output .write (chr ( value & 255 )) ; value = value // 256
14- output .write (chr ( value & 255 )) ; value = value // 256
15- output .write (chr ( value & 255 )) ; value = value // 256
16- output .write (chr ( value & 255 ))
13+ output .write (bytes ([ value & 255 ] )) ; value = value // 256
14+ output .write (bytes ([ value & 255 ] )) ; value = value // 256
15+ output .write (bytes ([ value & 255 ] )) ; value = value // 256
16+ output .write (bytes ([ value & 255 ] ))
1717
1818def read32 (input ):
1919 v = ord (input .read (1 ))
@@ -22,33 +22,34 @@ def read32(input):
2222 v += (ord (input .read (1 )) << 24 )
2323 return v
2424
25- def compress (filename , input , output ):
26- output .write ('\037 \213 \010 ' ) # Write the header, ...
27- output .write (chr ( FNAME )) # ... flag byte ...
25+ def compress (filename , input , output ):
26+ output .write (b '\037 \213 \010 ' ) # Write the header, ...
27+ output .write (bytes ([ FNAME ])) # ... flag byte ...
2828
29- statval = os .stat (filename ) # ... modification time ...
29+ statval = os .stat (filename ) # ... modification time ...
3030 mtime = statval [8 ]
3131 write32 (output , mtime )
32- output .write ('\002 ' ) # ... slowest compression alg. ...
33- output .write ('\377 ' ) # ... OS (=unknown) ...
34- output .write (filename + '\000 ' ) # ... original filename ...
32+ output .write (b'\002 ' ) # ... slowest compression alg. ...
33+ output .write (b'\377 ' ) # ... OS (=unknown) ...
34+ bfilename = filename .encode (sys .getfilesystemencoding ())
35+ output .write (bfilename + b'\000 ' ) # ... original filename ...
3536
36- crcval = zlib .crc32 ("" )
37+ crcval = zlib .crc32 (b'' )
3738 compobj = zlib .compressobj (9 , zlib .DEFLATED , - zlib .MAX_WBITS ,
3839 zlib .DEF_MEM_LEVEL , 0 )
3940 while True :
4041 data = input .read (1024 )
41- if data == "" :
42+ if data == b'' :
4243 break
4344 crcval = zlib .crc32 (data , crcval )
4445 output .write (compobj .compress (data ))
4546 output .write (compobj .flush ())
4647 write32 (output , crcval ) # ... the CRC ...
4748 write32 (output , statval [6 ]) # and the file size.
4849
49- def decompress (input , output ):
50+ def decompress (input , output ):
5051 magic = input .read (2 )
51- if magic != '\037 \213 ' :
52+ if magic != b '\037 \213 ' :
5253 print ('Not a gzipped file' )
5354 sys .exit (0 )
5455 if ord (input .read (1 )) != 8 :
@@ -66,21 +67,21 @@ def decompress (input, output):
6667 # Read and discard a null-terminated string containing the filename
6768 while True :
6869 s = input .read (1 )
69- if s == '\0 ' : break
70+ if s == b '\0 ' : break
7071 if flag & FCOMMENT :
7172 # Read and discard a null-terminated string containing a comment
7273 while True :
73- s = input .read (1 )
74- if s == '\0 ' : break
74+ s = input .read (1 )
75+ if s == b '\0 ' : break
7576 if flag & FHCRC :
7677 input .read (2 ) # Read & discard the 16-bit header CRC
7778
7879 decompobj = zlib .decompressobj (- zlib .MAX_WBITS )
79- crcval = zlib .crc32 ("" )
80+ crcval = zlib .crc32 (b'' )
8081 length = 0
8182 while True :
82- data = input .read (1024 )
83- if data == "" :
83+ data = input .read (1024 )
84+ if data == b "" :
8485 break
8586 decompdata = decompobj .decompress (data )
8687 output .write (decompdata )
0 commit comments