Make minigzip work again.

This commit is contained in:
Georg Brandl 2010-08-02 23:13:24 +00:00
parent 5ec5fed487
commit fb3b12d0c0

View file

@ -10,10 +10,10 @@ import zlib, sys, os
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
def write32(output, value): def write32(output, value):
output.write(chr(value & 255)) ; value=value // 256 output.write(bytes([value & 255])) ; value=value // 256
output.write(chr(value & 255)) ; value=value // 256 output.write(bytes([value & 255])) ; value=value // 256
output.write(chr(value & 255)) ; value=value // 256 output.write(bytes([value & 255])) ; value=value // 256
output.write(chr(value & 255)) output.write(bytes([value & 255]))
def read32(input): def read32(input):
v = ord(input.read(1)) v = ord(input.read(1))
@ -23,22 +23,23 @@ def read32(input):
return v return v
def compress(filename, input, output): def compress(filename, input, output):
output.write('\037\213\010') # Write the header, ... output.write(b'\037\213\010') # Write the header, ...
output.write(chr(FNAME)) # ... flag byte ... output.write(bytes([FNAME])) # ... flag byte ...
statval = os.stat(filename) # ... modification time ... statval = os.stat(filename) # ... modification time ...
mtime = statval[8] mtime = statval[8]
write32(output, mtime) write32(output, mtime)
output.write('\002') # ... slowest compression alg. ... output.write(b'\002') # ... slowest compression alg. ...
output.write('\377') # ... OS (=unknown) ... output.write(b'\377') # ... OS (=unknown) ...
output.write(filename+'\000') # ... original filename ... bfilename = filename.encode(sys.getfilesystemencoding())
output.write(bfilename + b'\000') # ... original filename ...
crcval = zlib.crc32("") crcval = zlib.crc32(b'')
compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL, 0) zlib.DEF_MEM_LEVEL, 0)
while True: while True:
data = input.read(1024) data = input.read(1024)
if data == "": if data == b'':
break break
crcval = zlib.crc32(data, crcval) crcval = zlib.crc32(data, crcval)
output.write(compobj.compress(data)) output.write(compobj.compress(data))
@ -48,7 +49,7 @@ def compress (filename, input, output):
def decompress(input, output): def decompress(input, output):
magic = input.read(2) magic = input.read(2)
if magic != '\037\213': if magic != b'\037\213':
print('Not a gzipped file') print('Not a gzipped file')
sys.exit(0) sys.exit(0)
if ord(input.read(1)) != 8: if ord(input.read(1)) != 8:
@ -66,21 +67,21 @@ def decompress (input, output):
# Read and discard a null-terminated string containing the filename # Read and discard a null-terminated string containing the filename
while True: while True:
s = input.read(1) s = input.read(1)
if s == '\0': break if s == b'\0': break
if flag & FCOMMENT: if flag & FCOMMENT:
# Read and discard a null-terminated string containing a comment # Read and discard a null-terminated string containing a comment
while True: while True:
s = input.read(1) s = input.read(1)
if s=='\0': break if s == b'\0': break
if flag & FHCRC: if flag & FHCRC:
input.read(2) # Read & discard the 16-bit header CRC input.read(2) # Read & discard the 16-bit header CRC
decompobj = zlib.decompressobj(-zlib.MAX_WBITS) decompobj = zlib.decompressobj(-zlib.MAX_WBITS)
crcval = zlib.crc32("") crcval = zlib.crc32(b'')
length = 0 length = 0
while True: while True:
data = input.read(1024) data = input.read(1024)
if data == "": if data == b"":
break break
decompdata = decompobj.decompress(data) decompdata = decompobj.decompress(data)
output.write(decompdata) output.write(decompdata)