使用python zlib解压缩使用javascript zlib压缩的字符串

时间:2017-09-25 12:33:20

标签: javascript python zlib

在客户端,我使用zlib.js库来缩小某些字符串,然后将其编码为base64以发送到服务器:

!SESSION 2017-09-25 14:20:42.193 -----------------------------------------------
eclipse.buildId=3.9.0.201707061823-RELEASE-e47
java.version=9
java.vendor=Oracle Corporation

在服务器端,我想使用zlib for python27进行解压缩,但是我收到以下错误:

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"

实现这一目标的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

zlib.js&#39; Deflate接受Array.<number>Uint8Array,您需要将JSON转换为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);

产生:eJwFgEEJAAAIA7tcDNvow+/A77D7MCtRTB8fHXoENA==