1

On the client side, I use the zlib.js library to deflate some string, then I encode it into base64 to be sent to the server:

var a = {"foo" : "bar"};
var deflate = new Zlib.Deflate(JSON.stringify(a));
var compressed = deflate.compress();
var sentToServer = btoa(String.fromCharCode.apply(null, compressed));
>>> "eJwVwwEJAAAAgKD/ry3B5QAdegQ0"

On the server side I want to use zlib for python27 to decompress, but I got the following error:

import base64, zlib

a = base64.b4decode("eJwVwwEJAAAAgKD/ry3B5QAdegQ0")
zlib.decompress(a)
>>>zlib.error: Error -3 while decompressing data: invalid distance too far back

What is the proper way to achieve that?

1 Answer 1

1

zlib.js's Deflate accepts Array.<number> orUint8Array, you need convert JSON to an array of int:

var a = {"foo" : "bar"};
var s = JSON.stringify(a);
var data = new Array(s.length);
for (i = 0, il = s.length; i < il; ++i) {
    data[i] = s.charCodeAt(i);
}
var deflate = new Zlib.Deflate(data);

which yields: eJwFgEEJAAAIA7tcDNvow+/A77D7MCtRTB8fHXoENA==

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.