javascriptpythoncompressiongzipzlib

Python zlib gzip decompression fails with string compressed on client side via pako


I want to decompress a JSON object on server (with Python's zlib) side that I gzipped on in the browser with the JS library pako. The compressed string is sent to the server via XHR. Unfortunately the server-side Python application fails with

Error -3 while decompressing: incorrect header check

The code I use for client-side compression:

var json = JSON.stringify(data);
var zlibOpts = {
    level: 9,
    to: 'string'
};
data = pako.gzip(json, zlibOpts);

The server-side code:

zlib_window_size = 31
result = zlib.decompressobj().decompress(data, zlib_window_size)

I figured out that the windowBits is important for zlib to recognize the string as being gzip-compressed (as stated in this thread and the zlib docs) so I used the same which paco uses for compression (31).

I also tried without any windowBits and with zlib.MAX_WBITS|16 or zlib.MAX_WBITS|32 in Python, which fails with the same error.

The server side application is running on Tornado.

Any help/hint is greatly appreciated!


Solution

  • Move the zlib_window_size from decompress() to decompressobj(). Or just get rid of the decompressobj(). You don't need it for a single call of decompress().