基于pako.js实现gzip的压缩和解压功能⽰例
本⽂实例讲述了基于pako.js实现gzip的压缩和解压功能。分享给⼤家供⼤家参考,具体如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>pako.js实现gzip的压缩和解压</title>
</head>
<body>
<script type="text/javascript" src="pako.js"></script>
<script >
var b64Data = 'H4sIAAAAAAAAAJ3UMQ7CMAwF0KugP2ewEzdpcxXUAbWAOiHUMqCqdyeVQAobfGXIYL8hP5ZXnEdkeNEk6vUgXTbLonC4zMjHFY/5Wm511ekdTsOCLKVp2rlIKOA2jTuBot/cr7BhobEwsbAloY8kDGyqoQ5H/oHsdwQ21cCmaspCz0L2jcYOgLH var ticketData = '{"ed":"20170121 09:44:01","fs":[{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"003","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"005","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"004","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"007","oids":["1"]},{"u // Output to console
var s = unzip(b64Data);
console.log("unzipped:");
console.log(s);
var data = zip(ticketData);
console.log("zipped:");
console.log(data);
function unzip(b64Data){
var strData = atob(b64Data);
// Convert binary string to character-number array
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});
// Turn number array into byte-array
var binData = new Uint8Array(charData);
// // unzip
var data = pako.inflate(binData);
// Convert gunzipped byteArray back to ascii string:
strData = String.fromCharCode.apply(null, new Uint16Array(data));
return strData;
}
function zip(str){
var binaryString = ip(str, { to: 'string' });
return btoa(binaryString);
}
</script>
</body>
</html>
运⾏效果图如下:
或者点击此处。
更多关于JavaScript相关内容可查看本站专题:《》、《》、《》、《》、《》及《》
希望本⽂所述对⼤家JavaScript程序设计有所帮助。
>代码运行js特效
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论