Decompression with zlib

Albert Hopkins marduk at letterboxes.org
Thu Apr 9 01:08:12 CEST 2009


On Wed, 2009-04-08 at 23:51 +0200, Emma Li wrote:
> Hello,
> 
> I'm trying to do compression/decompression of stuff with zlib, and I  
> just don't get it...
> Here is an example. I assume that dec should be "a", but it isn't. dec  
> turns out to be an empty string, and I don't understand why...
> 
> ===================
> import zlib
> compressor = zlib.compressobj(6) # level
> decompressor = zlib.decompressobj() # wbits
> 
> cmpr = compressor.compress("a")
> print "compressed", len(cmpr)
> dec = decompressor.decompress(cmpr)
> print "decompressed", len(dec)
> 
> # unconsumed tail and unused data
> print "decompressor.unconsumed_tail", decompressor.unconsumed_tail
> ##print "decompressor.unused_data", decompressor.unused_data
> 
> dec += decompressor.flush()
> print "after flushing", len(dec)
> ===================
> 

It looks like you forgot to flush the compressor object. Try insead:

import zlib

compressor = zlib.compressobj(6)
decompressor = zlib.decompressobj()

cmpr = compressor.compress("a")
cmpr += compressor.flush()
print "compressed", len(cmpr)

dec = decompressor.decompress(cmpr)
dec += decompressor.flush()
print "decompressed", len(dec)





More information about the Python-list mailing list