summaryrefslogtreecommitdiffstats
path: root/Demo/zlib/zlibdemo.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-05-28 16:13:21 (GMT)
committerGuido van Rossum <guido@python.org>1997-05-28 16:13:21 (GMT)
commit5416a0d97c17752dd9e83ba13bcfefccbb5d76fe (patch)
tree48bac73753ee1103b29a7e64c40264460a180a3a /Demo/zlib/zlibdemo.py
parent68bdad10154a555150089f4dde167ca8a3c49102 (diff)
downloadcpython-5416a0d97c17752dd9e83ba13bcfefccbb5d76fe.zip
cpython-5416a0d97c17752dd9e83ba13bcfefccbb5d76fe.tar.gz
cpython-5416a0d97c17752dd9e83ba13bcfefccbb5d76fe.tar.bz2
Checking in zlib demo.
Diffstat (limited to 'Demo/zlib/zlibdemo.py')
-rwxr-xr-xDemo/zlib/zlibdemo.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Demo/zlib/zlibdemo.py b/Demo/zlib/zlibdemo.py
new file mode 100755
index 0000000..fff1750
--- /dev/null
+++ b/Demo/zlib/zlibdemo.py
@@ -0,0 +1,36 @@
+#!/usr/local/bin/python
+
+import zlib, sys
+if len(sys.argv)>1: filename=sys.argv[1]
+else: filename='zlibdemo.py'
+print 'Reading', filename
+f=open(filename, 'r') # Get the data to compress
+s=f.read()
+f.close()
+
+# First, we'll compress the string in one step
+comptext=zlib.compress(s, 1)
+decomp=zlib.decompress(comptext)
+
+print '1-step compression: (level 1)'
+print ' Original:', len(s), 'Compressed:', len(comptext),
+print 'Uncompressed:', len(decomp)
+
+# Now, let's compress the string in stages; set chunk to work in smaller steps
+
+chunk=256
+compressor=zlib.compressobj(9)
+decompressor=zlib.decompressobj()
+comptext=decomp=''
+for i in range(0, len(s), chunk):
+ comptext=comptext+compressor.compress(s[i:i+chunk])
+comptext=comptext+compressor.flush() # Don't forget to call flush()!!
+
+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)
+