Kaydet (Commit) 35506135 authored tarafından Andrew M. Kuchling's avatar Andrew M. Kuchling

Put code in a main() function; loosen up the spacing to match current code style

üst 3725dea9
#!/usr/bin/env python #!/usr/bin/env python
# Takes an optional filename, defaulting to this file itself.
# Reads the file and compresses the content using level 1 and level 9
# compression, printing a summary of the results.
import zlib, sys import zlib, sys
if len(sys.argv)>1: filename=sys.argv[1]
else: filename='zlibdemo.py' def main():
print 'Reading', filename if len(sys.argv) > 1:
f=open(filename, 'r') # Get the data to compress filename = sys.argv[1]
s=f.read() else:
f.close() filename = sys.argv[0]
print 'Reading', filename
# First, we'll compress the string in one step
comptext=zlib.compress(s, 1) f = open(filename, 'rb') # Get the data to compress
decomp=zlib.decompress(comptext) s = f.read()
f.close()
print '1-step compression: (level 1)'
print ' Original:', len(s), 'Compressed:', len(comptext), # First, we'll compress the string in one step
print 'Uncompressed:', len(decomp) comptext = zlib.compress(s, 1)
decomp = zlib.decompress(comptext)
# Now, let's compress the string in stages; set chunk to work in smaller steps
print '1-step compression: (level 1)'
chunk=256 print ' Original:', len(s), 'Compressed:', len(comptext),
compressor=zlib.compressobj(9) print 'Uncompressed:', len(decomp)
decompressor=zlib.decompressobj()
comptext=decomp='' # Now, let's compress the string in stages; set chunk to work in smaller steps
for i in range(0, len(s), chunk):
comptext=comptext+compressor.compress(s[i:i+chunk]) chunk = 256
comptext=comptext+compressor.flush() # Don't forget to call flush()!! compressor = zlib.compressobj(9)
decompressor = zlib.decompressobj()
for i in range(0, len(comptext), chunk): comptext = decomp = ''
decomp=decomp+decompressor.decompress(comptext[i:i+chunk]) for i in range(0, len(s), chunk):
decomp=decomp+decompressor.flush() comptext = comptext+compressor.compress(s[i:i+chunk])
# Don't forget to call flush()!!
print 'Progressive compression (level 9):' comptext = comptext + compressor.flush()
print ' Original:', len(s), 'Compressed:', len(comptext),
print 'Uncompressed:', len(decomp) for i in range(0, len(comptext), chunk):
decomp = decomp + decompressor.decompress(comptext[i:i+chunk])
decomp=decomp+decompressor.flush()
print 'Progressive compression (level 9):'
print ' Original:', len(s), 'Compressed:', len(comptext),
print 'Uncompressed:', len(decomp)
if __name__ == '__main__':
main()
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