在服务器端,我使用python zlib压缩字符串,如下所示:
import zlib
s = '{"foo": "bar"}'
compress = zlib.compress(s)
print compress
上一代码的结果如下
xœ«VJËÏW²RPJJ,Rª
在客户端,我使用zlib.js解压缩
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);
我收到以下错误
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
我做错了什么?
答案 0 :(得分:0)
问题在于编码。在python中我用base64编码。
>>> import zlib
>>> s = '{"foo": "bar"}'
>>> compress = zlib.compress(s)
>>> print compress.encode('base64')
>>> "eJyrVkrLz1eyUlBKSixSqgUAIJgEVA=="
在客户端:
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"}"
非常感谢你的帮助