Commit fb3b12d0 authored by Georg Brandl's avatar Georg Brandl

Make minigzip work again.

parent 5ec5fed4
...@@ -10,10 +10,10 @@ import zlib, sys, os ...@@ -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))
...@@ -22,23 +22,24 @@ def read32(input): ...@@ -22,23 +22,24 @@ def read32(input):
v += (ord(input.read(1)) << 24) v += (ord(input.read(1)) << 24)
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))
...@@ -46,9 +47,9 @@ def compress (filename, input, output): ...@@ -46,9 +47,9 @@ def compress (filename, input, output):
write32(output, crcval) # ... the CRC ... write32(output, crcval) # ... the CRC ...
write32(output, statval[6]) # and the file size. write32(output, statval[6]) # and the file size.
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): ...@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment