Fork me on GitHub

js zlib 压缩

使用 zlib.js 实现 js zlib 解压/压缩

python

1
2
3
4
5
6
7
8
python
import zlib
from base64 import urlsafe_b64encode
from base64 import urlsafe_b64decode
print urlsafe_b64encode(zlib.compress('li:8,tj:mixing'))
# 输出:eJzLybSy0CnJssrNrMjMSwcAJBYFGA==
print zlib.decompress(urlsafe_b64decode('eJzLybSy0CnJssrNrMjMSwcAJBYFGA=='))
# 输出:li:8,tj:mixing

js 对 python 的压缩结果解压

1
2
3
4
5
6
7
8
9
10
var zlib_result = atob("eJzLybSy0CnJssrNrMjMSwcAJBYFGA==");
var data = new Array(zlib_result.length);
for (i = 0, il = zlib_result.length; i < il; ++i) {
data[i] = zlib_result.charCodeAt(i);
}
var inflate = new Zlib.Inflate(data);
var decompress = inflate.decompress();
var plain = new TextDecoder("utf-8").decode(decompress);
console.log(plain);
// 输出:li:8,tj:mixing

js 压缩同样的内容

1
2
3
4
5
6
7
8
9
10
11
var str = 'li:8,tj:mixing';
var plain = new TextEncoder('utf-8').encode(str);
var deflate = new Zlib.Deflate(plain);
var compressed = deflate.compress();
var data = [];
for(var i = 0, len = compressed.length; i < len; i++){
data.push(String.fromCharCode(compressed[i]));
}
data = data.join("");
console.log(btoa(data));
// 输出:eJwFgEEJAEAIBAsZQLaQzKG+fFz8pVHGPQ2fLQMkFgUY

js zlib 与 pythod 加密后不一样,但用 python 可以解出来

1
2
print zlib.decompress(urlsafe_b64decode('eJwFgEEJAEAIBAsZQLaQzKG+fFz8pVHGPQ2fLQMkFgUY'))
# 输出:li:8,tj:mixing

js 对 js 的压缩结果解压

1
2
3
4
5
6
7
8
9
10
var zlib_result = atob("eJwFgEEJAEAIBAsZQLaQzKG+fFz8pVHGPQ2fLQMkFgUY");
var data = new Array(zlib_result.length);
for (i = 0, il = zlib_result.length; i < il; ++i) {
data[i] = zlib_result.charCodeAt(i);
}
var inflate = new Zlib.Inflate(data);
var decompress = inflate.decompress();
var plain = new TextDecoder("utf-8").decode(decompress);
console.log(plain);
// 输出:li:8,tj:mixing
-------------感谢您的阅读 有问题请留言(或mailto:frostbelt@sina.cn)-------------