Skip to content
Advertisement

Use zlib.js to decompress python zlib compress

On the server side I use python zlib to compress a string as follows:

import zlib

s = '{"foo": "bar"}'
compress = zlib.compress(s)
print compress

The result of the previous code is the following

xœ«VJËÏW²RPJJ,Rª

On the client side I use zlib.js to decompress

var s = "xœ«VJËÏW²RPJJ,Rª"
var data = new Array(s.length);
for (i = 0, il = s.length; i < il; ++i) {
    data[i] = s.charCodeAt(i);
}
var inflate = new Zlib.Inflate(data);

I get the following error

zlib_and_gzip.min.js:1 Uncaught Error: invalid fcheck flag:28
    at new tb (zlib_and_gzip.min.js:48)
    at <anonymous>:1:15

what am I doing wrong?

Advertisement

Answer

The problem was coding. in python I used base64 to encode.

>>> import zlib

>>> s = '{"foo": "bar"}'
>>> compress = zlib.compress(s)
>>> print compress.encode('base64')

>>> "eJyrVkrLz1eyUlBKSixSqgUAIJgEVA=="

On the client side:

var s = atob("eJyrVkrLz1eyUlBKSixSqgUAIJgEVA==");

var data = new Array(s.length);
for (i = 0, il = s.length; i < il; ++i) {
    data[i] = s.charCodeAt(i);
}

var inflate = new Zlib.Inflate(data);
var decompress = inflate.decompress();
var plain = new TextDecoder("utf-8").decode(decompress);

plain 
'{"foo": "bar"}'

Thank you very much for the help

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement