Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5416a0d

Browse files
committed
Checking in zlib demo.
1 parent 68bdad1 commit 5416a0d

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Demo/zlib/minigzip.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/local/bin/python
2+
# Demo program for zlib; it compresses or decompresses files, but *doesn't*
3+
# delete the original. This doesn't support all of gzip's options.
4+
5+
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
6+
7+
def write32(output, value):
8+
output.write(chr(value & 255)) ; value=value / 256
9+
output.write(chr(value & 255)) ; value=value / 256
10+
output.write(chr(value & 255)) ; value=value / 256
11+
output.write(chr(value & 255))
12+
13+
def read32(input):
14+
v=ord(input.read(1))
15+
v=v+ (ord(input.read(1))<<8 )
16+
v=v+ (ord(input.read(1))<<16)
17+
v=v+ (ord(input.read(1))<<24)
18+
return v
19+
20+
import zlib, sys
21+
if len(sys.argv)!=2:
22+
print 'Usage: minigzip.py <filename>'
23+
print ' The file will be compressed or decompressed.'
24+
sys.exit(0)
25+
26+
filename=sys.argv[1]
27+
compressing=1 ; outputname=filename+'.gz'
28+
if filename[-3:]=='.gz':
29+
compressing=0 ; outputname=filename[:-3]
30+
input=open(filename) ; output=open(outputname, 'w')
31+
32+
if compressing:
33+
output.write('\037\213\010') # Write the header, ...
34+
output.write(chr(FNAME)) # ... flag byte ...
35+
36+
import os # ... modification time ...
37+
statval=os.stat(filename)
38+
mtime=statval[8]
39+
write32(output, mtime)
40+
output.write('\002') # ... slowest compression alg. ...
41+
output.write('\377') # ... OS (=unknown) ...
42+
output.write(filename+'\000') # ... original filename ...
43+
44+
crcval=zlib.crc32("")
45+
compobj=zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,
46+
zlib.DEF_MEM_LEVEL, 0)
47+
while (1):
48+
data=input.read(1024)
49+
if data=="": break
50+
crcval=zlib.crc32(data, crcval)
51+
output.write(compobj.compress(data))
52+
output.write(compobj.flush())
53+
write32(output, crcval) # ... the CRC ...
54+
write32(output, statval[6]) # and the file size.
55+
56+
else:
57+
magic=input.read(2)
58+
if magic!='\037\213':
59+
print 'Not a gzipped file' ; sys.exit(0)
60+
if ord(input.read(1))!=8:
61+
print 'Unknown compression method' ; sys.exit(0)
62+
flag=ord(input.read(1))
63+
input.read(4+1+1) # Discard modification time,
64+
# extra flags, and OS byte.
65+
if flag & FEXTRA:
66+
# Read & discard the extra field, if present
67+
xlen=ord(input.read(1))
68+
xlen=xlen+256*ord(input.read(1))
69+
input.read(xlen)
70+
if flag & FNAME:
71+
# Read and discard a null-terminated string containing the filename
72+
while (1):
73+
s=input.read(1)
74+
if s=='\000': break
75+
if flag & FCOMMENT:
76+
# Read and discard a null-terminated string containing a comment
77+
while (1):
78+
s=input.read(1)
79+
if s=='\000': break
80+
if flag & FHCRC:
81+
input.read(2) # Read & discard the 16-bit header CRC
82+
decompobj=zlib.decompressobj(-zlib.MAX_WBITS)
83+
crcval=zlib.crc32("")
84+
length=0
85+
while (1):
86+
data=input.read(1024)
87+
if data=="": break
88+
decompdata=decompobj.decompress(data)
89+
print len(decompdata)
90+
output.write(decompdata) ; length=length+len(decompdata)
91+
crcval=zlib.crc32(decompdata, crcval)
92+
decompdata=decompobj.flush()
93+
output.write(decompdata) ; length=length+len(decompdata)
94+
crcval=zlib.crc32(decompdata, crcval)
95+
96+
# We've read to the end of the file, so we have to rewind in order
97+
# to reread the 8 bytes containing the CRC and the file size. The
98+
# decompressor is smart and knows when to stop, so feeding it
99+
# extra data is harmless.
100+
input.seek(-8, 2)
101+
crc32=read32(input)
102+
isize=read32(input)
103+
if crc32!=crcval: print 'CRC check failed.'
104+
if isize!=length: print 'Incorrect length of data produced'
105+
106+
input.close() ; output.close()
107+

Demo/zlib/zlibdemo.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/local/bin/python
2+
3+
import zlib, sys
4+
if len(sys.argv)>1: filename=sys.argv[1]
5+
else: filename='zlibdemo.py'
6+
print 'Reading', filename
7+
f=open(filename, 'r') # Get the data to compress
8+
s=f.read()
9+
f.close()
10+
11+
# First, we'll compress the string in one step
12+
comptext=zlib.compress(s, 1)
13+
decomp=zlib.decompress(comptext)
14+
15+
print '1-step compression: (level 1)'
16+
print ' Original:', len(s), 'Compressed:', len(comptext),
17+
print 'Uncompressed:', len(decomp)
18+
19+
# Now, let's compress the string in stages; set chunk to work in smaller steps
20+
21+
chunk=256
22+
compressor=zlib.compressobj(9)
23+
decompressor=zlib.decompressobj()
24+
comptext=decomp=''
25+
for i in range(0, len(s), chunk):
26+
comptext=comptext+compressor.compress(s[i:i+chunk])
27+
comptext=comptext+compressor.flush() # Don't forget to call flush()!!
28+
29+
for i in range(0, len(comptext), chunk):
30+
decomp=decomp+decompressor.decompress(comptext[i:i+chunk])
31+
decomp=decomp+decompressor.flush()
32+
33+
print 'Progressive compression (level 9):'
34+
print ' Original:', len(s), 'Compressed:', len(comptext),
35+
print 'Uncompressed:', len(decomp)
36+

0 commit comments

Comments
 (0)