mirror of https://github.com/python/cpython
Use functions; modernize code
This commit is contained in:
parent
3550613502
commit
0b4e554be5
|
@ -1,106 +1,133 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# Demo program for zlib; it compresses or decompresses files, but *doesn't*
|
# Demo program for zlib; it compresses or decompresses files, but *doesn't*
|
||||||
# delete the original. This doesn't support all of gzip's options.
|
# delete the original. This doesn't support all of gzip's options.
|
||||||
|
#
|
||||||
|
# The 'gzip' module in the standard library provides a more complete
|
||||||
|
# implementation of gzip-format files.
|
||||||
|
|
||||||
|
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(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)) ; value=value / 256
|
output.write(chr(value & 255)) ; value=value // 256
|
||||||
output.write(chr(value & 255))
|
output.write(chr(value & 255))
|
||||||
|
|
||||||
def read32(input):
|
def read32(input):
|
||||||
v=ord(input.read(1))
|
v = ord(input.read(1))
|
||||||
v=v+ (ord(input.read(1))<<8 )
|
v += (ord(input.read(1)) << 8 )
|
||||||
v=v+ (ord(input.read(1))<<16)
|
v += (ord(input.read(1)) << 16)
|
||||||
v=v+ (ord(input.read(1))<<24)
|
v += (ord(input.read(1)) << 24)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
import zlib, sys
|
def compress (filename, input, output):
|
||||||
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, ...
|
output.write('\037\213\010') # Write the header, ...
|
||||||
output.write(chr(FNAME)) # ... flag byte ...
|
output.write(chr(FNAME)) # ... flag byte ...
|
||||||
|
|
||||||
import os # ... modification time ...
|
statval = os.stat(filename) # ... modification time ...
|
||||||
statval=os.stat(filename)
|
mtime = statval[8]
|
||||||
mtime=statval[8]
|
|
||||||
write32(output, mtime)
|
write32(output, mtime)
|
||||||
output.write('\002') # ... slowest compression alg. ...
|
output.write('\002') # ... slowest compression alg. ...
|
||||||
output.write('\377') # ... OS (=unknown) ...
|
output.write('\377') # ... OS (=unknown) ...
|
||||||
output.write(filename+'\000') # ... original filename ...
|
output.write(filename+'\000') # ... original filename ...
|
||||||
|
|
||||||
crcval=zlib.crc32("")
|
crcval = zlib.crc32("")
|
||||||
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 (1):
|
while True:
|
||||||
data=input.read(1024)
|
data = input.read(1024)
|
||||||
if data=="": break
|
if data == "":
|
||||||
crcval=zlib.crc32(data, crcval)
|
break
|
||||||
|
crcval = zlib.crc32(data, crcval)
|
||||||
output.write(compobj.compress(data))
|
output.write(compobj.compress(data))
|
||||||
output.write(compobj.flush())
|
output.write(compobj.flush())
|
||||||
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.
|
||||||
|
|
||||||
else:
|
def decompress (input, output):
|
||||||
magic=input.read(2)
|
magic = input.read(2)
|
||||||
if magic!='\037\213':
|
if magic != '\037\213':
|
||||||
print 'Not a gzipped file' ; sys.exit(0)
|
print 'Not a gzipped file'
|
||||||
if ord(input.read(1))!=8:
|
sys.exit(0)
|
||||||
print 'Unknown compression method' ; sys.exit(0)
|
if ord(input.read(1)) != 8:
|
||||||
flag=ord(input.read(1))
|
print 'Unknown compression method'
|
||||||
|
sys.exit(0)
|
||||||
|
flag = ord(input.read(1))
|
||||||
input.read(4+1+1) # Discard modification time,
|
input.read(4+1+1) # Discard modification time,
|
||||||
# extra flags, and OS byte.
|
# extra flags, and OS byte.
|
||||||
if flag & FEXTRA:
|
if flag & FEXTRA:
|
||||||
# Read & discard the extra field, if present
|
# Read & discard the extra field, if present
|
||||||
xlen=ord(input.read(1))
|
xlen = ord(input.read(1))
|
||||||
xlen=xlen+256*ord(input.read(1))
|
xlen += 256*ord(input.read(1))
|
||||||
input.read(xlen)
|
input.read(xlen)
|
||||||
if flag & FNAME:
|
if flag & FNAME:
|
||||||
# Read and discard a null-terminated string containing the filename
|
# Read and discard a null-terminated string containing the filename
|
||||||
while (1):
|
while True:
|
||||||
s=input.read(1)
|
s = input.read(1)
|
||||||
if s=='\000': break
|
if s == '\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 (1):
|
while True:
|
||||||
s=input.read(1)
|
s=input.read(1)
|
||||||
if s=='\000': break
|
if s=='\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)
|
|
||||||
crcval=zlib.crc32("")
|
decompobj = zlib.decompressobj(-zlib.MAX_WBITS)
|
||||||
length=0
|
crcval = zlib.crc32("")
|
||||||
while (1):
|
length = 0
|
||||||
|
while True:
|
||||||
data=input.read(1024)
|
data=input.read(1024)
|
||||||
if data=="": break
|
if data == "":
|
||||||
decompdata=decompobj.decompress(data)
|
break
|
||||||
print len(decompdata)
|
decompdata = decompobj.decompress(data)
|
||||||
output.write(decompdata) ; length=length+len(decompdata)
|
output.write(decompdata)
|
||||||
crcval=zlib.crc32(decompdata, crcval)
|
length += len(decompdata)
|
||||||
decompdata=decompobj.flush()
|
crcval = zlib.crc32(decompdata, crcval)
|
||||||
output.write(decompdata) ; length=length+len(decompdata)
|
|
||||||
crcval=zlib.crc32(decompdata, crcval)
|
decompdata = decompobj.flush()
|
||||||
|
output.write(decompdata)
|
||||||
|
length += len(decompdata)
|
||||||
|
crcval = zlib.crc32(decompdata, crcval)
|
||||||
|
|
||||||
# We've read to the end of the file, so we have to rewind in order
|
# 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
|
# to reread the 8 bytes containing the CRC and the file size. The
|
||||||
# decompressor is smart and knows when to stop, so feeding it
|
# decompressor is smart and knows when to stop, so feeding it
|
||||||
# extra data is harmless.
|
# extra data is harmless.
|
||||||
input.seek(-8, 2)
|
input.seek(-8, 2)
|
||||||
crc32=read32(input)
|
crc32 = read32(input)
|
||||||
isize=read32(input)
|
isize = read32(input)
|
||||||
if crc32!=crcval: print 'CRC check failed.'
|
if crc32 != crcval:
|
||||||
if isize!=length: print 'Incorrect length of data produced'
|
print 'CRC check failed.'
|
||||||
|
if isize != length:
|
||||||
|
print 'Incorrect length of data produced'
|
||||||
|
|
||||||
input.close() ; output.close()
|
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()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue