Zlib: correct checksum but error decompressing

InvisibleRoads Patrol patrol at invisibleroads.com
Wed Aug 26 11:01:37 EDT 2009


On Wed, 26 Aug 2009 07:19:42 -0700 (PDT), Andre <andre.cohen at gmail.com>
wrote:
> I have been trying to solve this issue for a while now. I receive data
> from a TCP connection which is compressed. I know the correct checksum
> for the data and both the client and server generate the same
> checksum. However, in Python when it comes to decompressing the data I
> get the exception: "Error -5 while decompressing data"! I would assume
> that if the string in python is equivalent to the correct checksum
> than the decompress function should also work on the same string, but
> that's clearly not the case.
> 
> # convert data to a byte array
> data = array('b', raw_data)
> # print checksum for visual inspection
> print zlib.crc32(data.tostring())
> # try to decompress, but fails!
> str = zlib.decompress(data.tostring())
> 
> Does anyone know what's going on?

Hi Andre,

Hmm.  Can you decompress the string on the server before it was sent? 
Maybe the zipfile or gzip module will work.
Reference:
http://bytes.com/topic/python/answers/42131-zlib-decompress-cannot-gunzip-can
from cStringIO import StringIO
from gzip import GzipFile
body = GzipFile('', 'r', 0, StringIO(raw_data)).read()

You might want to try experimenting with the wbits parameter of
zlib.decompress()
Reference:
http://mail.python.org/pipermail/python-list/2008-December/691694.html
zlib.decompress(data, -15)

The zlib module seems to work fine with both strings and byte arrays.
import array, zlib
dataAsString = zlib.compress('example string')
dataAsArray = array.array('b', dataAsString)
zlib.decompress(dataAsString) == zlib.decompress(dataAsArray)
zlib.decompress(dataAsString) == zlib.decompress(dataAsArray.tostring())

-- 
http://invisibleroads.com

We train ordinary people into Python software developers and connect them
with jobs and projects for local businesses.



More information about the Python-list mailing list