Another demo that at least runs again.

This commit is contained in:
Georg Brandl 2010-08-02 23:15:58 +00:00
parent fb3b12d0c0
commit 2db2b8a17d
1 changed files with 7 additions and 8 deletions

View File

@ -13,9 +13,8 @@ def main():
filename = sys.argv[0] filename = sys.argv[0]
print('Reading', filename) print('Reading', filename)
f = open(filename, 'rb') # Get the data to compress with open(filename, 'rb') as f: # Get the data to compress
s = f.read() s = f.read()
f.close()
# First, we'll compress the string in one step # First, we'll compress the string in one step
comptext = zlib.compress(s, 1) comptext = zlib.compress(s, 1)
@ -30,15 +29,15 @@ def main():
chunk = 256 chunk = 256
compressor = zlib.compressobj(9) compressor = zlib.compressobj(9)
decompressor = zlib.decompressobj() decompressor = zlib.decompressobj()
comptext = decomp = '' comptext = decomp = b''
for i in range(0, len(s), chunk): for i in range(0, len(s), chunk):
comptext = comptext+compressor.compress(s[i:i+chunk]) comptext += compressor.compress(s[i:i+chunk])
# Don't forget to call flush()!! # Don't forget to call flush()!!
comptext = comptext + compressor.flush() comptext += compressor.flush()
for i in range(0, len(comptext), chunk): for i in range(0, len(comptext), chunk):
decomp = decomp + decompressor.decompress(comptext[i:i+chunk]) decomp += decompressor.decompress(comptext[i:i+chunk])
decomp=decomp+decompressor.flush() decomp += decompressor.flush()
print('Progressive compression (level 9):') print('Progressive compression (level 9):')
print(' Original:', len(s), 'Compressed:', len(comptext), end=' ') print(' Original:', len(s), 'Compressed:', len(comptext), end=' ')