Zlib: correct checksum but error decompressing
patrol at invisibleroads.com
Wed Aug 26 17:01:37 CEST 2009
On Wed, 26 Aug 2009 07:19:42 -0700 (PDT), Andre <andre.cohen at gmail.com>
> 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?
Hmm. Can you decompress the string on the server before it was sent?
Maybe the zipfile or gzip module will work.
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
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())
We train ordinary people into Python software developers and connect them
with jobs and projects for local businesses.
More information about the Python-list