cpython/Demo/zlib/minigzip.py

108 lines
3.8 KiB
Python
Raw Normal View History

1997-05-28 13:13:21 -03:00
#!/usr/local/bin/python
# Demo program for zlib; it compresses or decompresses files, but *doesn't*
# delete the original. This doesn't support all of gzip's options.
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
def write32(output, value):
output.write(chr(value & 255)) ; value=value / 256
output.write(chr(value & 255)) ; value=value / 256
output.write(chr(value & 255)) ; value=value / 256
output.write(chr(value & 255))
def read32(input):
v=ord(input.read(1))
v=v+ (ord(input.read(1))<<8 )
v=v+ (ord(input.read(1))<<16)
v=v+ (ord(input.read(1))<<24)
return v
import zlib, sys
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]
compressing=1 ; outputname=filename+'.gz'
if filename[-3:]=='.gz':
compressing=0 ; outputname=filename[:-3]
input=open(filename) ; output=open(outputname, 'w')
if compressing:
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
1998-09-14 13:44:15 -03:00
import os # ... modification time ...
1997-05-28 13:13:21 -03:00
statval=os.stat(filename)
mtime=statval[8]
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
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)
1997-05-28 13:13:21 -03:00
while (1):
1998-09-14 13:44:15 -03:00
data=input.read(1024)
if data=="": break
crcval=zlib.crc32(data, crcval)
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
else:
magic=input.read(2)
if magic!='\037\213':
1998-09-14 13:44:15 -03:00
print 'Not a gzipped file' ; sys.exit(0)
1997-05-28 13:13:21 -03:00
if ord(input.read(1))!=8:
1998-09-14 13:44:15 -03:00
print 'Unknown compression method' ; sys.exit(0)
1997-05-28 13:13:21 -03:00
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
xlen=ord(input.read(1))
xlen=xlen+256*ord(input.read(1))
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
while (1):
s=input.read(1)
if s=='\000': 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
while (1):
s=input.read(1)
if s=='\000': 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
1997-05-28 13:13:21 -03:00
decompobj=zlib.decompressobj(-zlib.MAX_WBITS)
crcval=zlib.crc32("")
length=0
while (1):
1998-09-14 13:44:15 -03:00
data=input.read(1024)
if data=="": break
decompdata=decompobj.decompress(data)
print len(decompdata)
output.write(decompdata) ; length=length+len(decompdata)
crcval=zlib.crc32(decompdata, crcval)
1997-05-28 13:13:21 -03:00
decompdata=decompobj.flush()
output.write(decompdata) ; length=length+len(decompdata)
crcval=zlib.crc32(decompdata, crcval)
# 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.
input.seek(-8, 2)
crc32=read32(input)
isize=read32(input)
if crc32!=crcval: print 'CRC check failed.'
if isize!=length: print 'Incorrect length of data produced'
input.close() ; output.close()