On the server side I use python zlib to compress a string as follows:
JavaScript
x
6
1
import zlib
2
3
s = '{"foo": "bar"}'
4
compress = zlib.compress(s)
5
print compress
6
The result of the previous code is the following
JavaScript
1
2
1
xœ«VJËÏW²RPJJ,Rª
2
On the client side I use zlib.js to decompress
JavaScript
1
7
1
var s = "xœ«VJËÏW²RPJJ,Rª"
2
var data = new Array(s.length);
3
for (i = 0, il = s.length; i < il; ++i) {
4
data[i] = s.charCodeAt(i);
5
}
6
var inflate = new Zlib.Inflate(data);
7
I get the following error
JavaScript
1
4
1
zlib_and_gzip.min.js:1 Uncaught Error: invalid fcheck flag:28
2
at new tb (zlib_and_gzip.min.js:48)
3
at <anonymous>:1:15
4
what am I doing wrong?
Advertisement
Answer
The problem was coding. in python I used base64 to encode.
JavaScript
1
8
1
>>> import zlib
2
3
>>> s = '{"foo": "bar"}'
4
>>> compress = zlib.compress(s)
5
>>> print compress.encode('base64')
6
7
>>> "eJyrVkrLz1eyUlBKSixSqgUAIJgEVA=="
8
On the client side:
JavaScript
1
14
14
1
var s = atob("eJyrVkrLz1eyUlBKSixSqgUAIJgEVA==");
2
3
var data = new Array(s.length);
4
for (i = 0, il = s.length; i < il; ++i) {
5
data[i] = s.charCodeAt(i);
6
}
7
8
var inflate = new Zlib.Inflate(data);
9
var decompress = inflate.decompress();
10
var plain = new TextDecoder("utf-8").decode(decompress);
11
12
plain
13
'{"foo": "bar"}'
14
Thank you very much for the help