cpython/Demo/zlib/minigzip.py

134 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
1997-05-28 13:13:21 -03:00
# Demo program for zlib; it compresses or decompresses files, but *doesn't*
# delete the original. This doesn't support all of gzip's options.
2006-06-03 20:39:07 -03:00
#
# The 'gzip' module in the standard library provides a more complete
# implementation of gzip-format files.
import zlib, sys, os
1997-05-28 13:13:21 -03:00
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
def write32(output, value):
2006-06-03 20:39:07 -03:00
output.write(chr(value & 255)) ; value=value // 256
output.write(chr(value & 255)) ; value=value // 256
output.write(chr(value & 255)) ; value=value // 256
1997-05-28 13:13:21 -03:00
output.write(chr(value & 255))
1997-05-28 13:13:21 -03:00
def read32(input):
2006-06-03 20:39:07 -03:00
v = ord(input.read(1))
v += (ord(input.read(1)) << 8 )
v += (ord(input.read(1)) << 16)
v += (ord(input.read(1)) << 24)
1997-05-28 13:13:21 -03:00
return v
2006-06-03 20:39:07 -03:00
def compress (filename, input, output):
1997-05-28 13:13:21 -03:00
output.write('\037\213\010') # Write the header, ...
1998-09-14 13:44:15 -03:00
output.write(chr(FNAME)) # ... flag byte ...
1997-05-28 13:13:21 -03:00
2006-06-03 20:39:07 -03:00
statval = os.stat(filename) # ... modification time ...
mtime = statval[8]
1997-05-28 13:13:21 -03:00
write32(output, mtime)
1998-09-14 13:44:15 -03:00
output.write('\002') # ... slowest compression alg. ...
output.write('\377') # ... OS (=unknown) ...
output.write(filename+'\000') # ... original filename ...
1997-05-28 13:13:21 -03:00
2006-06-03 20:39:07 -03:00
crcval = zlib.crc32("")
compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
1998-09-14 13:44:15 -03:00
zlib.DEF_MEM_LEVEL, 0)
2006-06-03 20:39:07 -03:00
while True:
data = input.read(1024)
if data == "":
break
crcval = zlib.crc32(data, crcval)
1998-09-14 13:44:15 -03:00
output.write(compobj.compress(data))
1997-05-28 13:13:21 -03:00
output.write(compobj.flush())
1998-09-14 13:44:15 -03:00
write32(output, crcval) # ... the CRC ...
write32(output, statval[6]) # and the file size.
1997-05-28 13:13:21 -03:00
2006-06-03 20:39:07 -03:00
def decompress (input, output):
magic = input.read(2)
if magic != '\037\213':
print 'Not a gzipped file'
sys.exit(0)
if ord(input.read(1)) != 8:
print 'Unknown compression method'
sys.exit(0)
flag = ord(input.read(1))
1998-09-14 13:44:15 -03:00
input.read(4+1+1) # Discard modification time,
# extra flags, and OS byte.
1997-05-28 13:13:21 -03:00
if flag & FEXTRA:
1998-09-14 13:44:15 -03:00
# Read & discard the extra field, if present
2006-06-03 20:39:07 -03:00
xlen = ord(input.read(1))
xlen += 256*ord(input.read(1))
1998-09-14 13:44:15 -03:00
input.read(xlen)
1997-05-28 13:13:21 -03:00
if flag & FNAME:
1998-09-14 13:44:15 -03:00
# Read and discard a null-terminated string containing the filename
2006-06-03 20:39:07 -03:00
while True:
s = input.read(1)
if s == '\0': break
1997-05-28 13:13:21 -03:00
if flag & FCOMMENT:
1998-09-14 13:44:15 -03:00
# Read and discard a null-terminated string containing a comment
2006-06-03 20:39:07 -03:00
while True:
1998-09-14 13:44:15 -03:00
s=input.read(1)
2006-06-03 20:39:07 -03:00
if s=='\0': break
1997-05-28 13:13:21 -03:00
if flag & FHCRC:
1998-09-14 13:44:15 -03:00
input.read(2) # Read & discard the 16-bit header CRC
2006-06-03 20:39:07 -03:00
decompobj = zlib.decompressobj(-zlib.MAX_WBITS)
crcval = zlib.crc32("")
length = 0
while True:
1998-09-14 13:44:15 -03:00
data=input.read(1024)
2006-06-03 20:39:07 -03:00
if data == "":
break
decompdata = decompobj.decompress(data)
output.write(decompdata)
length += len(decompdata)
crcval = zlib.crc32(decompdata, crcval)
decompdata = decompobj.flush()
output.write(decompdata)
length += len(decompdata)
crcval = zlib.crc32(decompdata, crcval)
1997-05-28 13:13:21 -03:00
# We've read to the end of the file, so we have to rewind in order
# to reread the 8 bytes containing the CRC and the file size. The
# decompressor is smart and knows when to stop, so feeding it
# extra data is harmless.
1997-05-28 13:13:21 -03:00
input.seek(-8, 2)
2006-06-03 20:39:07 -03:00
crc32 = read32(input)
isize = read32(input)
if crc32 != crcval:
print 'CRC check failed.'
if isize != length:
print 'Incorrect length of data produced'
def main():
if len(sys.argv)!=2:
print 'Usage: minigzip.py <filename>'
print ' The file will be compressed or decompressed.'
sys.exit(0)
filename = sys.argv[1]
if filename.endswith('.gz'):
compressing = False
outputname = filename[:-3]
else:
compressing = True
outputname = filename + '.gz'
input = open(filename, 'rb')
output = open(outputname, 'wb')
if compressing:
compress(filename, input, output)
else:
decompress(input, output)
input.close()
output.close()
1997-05-28 13:13:21 -03:00
2006-06-03 20:39:07 -03:00
if __name__ == '__main__':
main()