3

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?

2
  • 1
    Double-check your encoding? using a string might change byte-order or add unexpected bits? You might consider using base64 for transmission to reduce such errors. Commented Nov 1, 2017 at 15:09
  • Definately an encoding problem. The string should be var s = 'x\x9c\xabVJ\xcb\xcfW\xb2RPJJ,R\xaa\x05\x00 \x98\x04T' Commented Nov 1, 2017 at 15:26

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.