The content of this article is about the code implementation of gzip decompression in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The code is as follows:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/pako/1.0.6/pako.min.js"></script> </head> <body> <input id="content" type="text"> <button onclick="encode()">encode</button> <button onclick="decode()">decode</button> <div id="ciphertext"></div> </body> </html> <script type="text/javascript"> function encode(){ var str = $('#content').val(); str = window.btoa(pako.gzip(str, {to: "string"})) $('#ciphertext').text(str); } function decode(){ var encodedData = $('#content').val(); var decodedData = window.atob(encodedData); var charData = decodedData.split('').map(function(x){return x.charCodeAt(0);}); var binData = new Uint8Array(charData); var data = pako.inflate(binData); decodedData = String.fromCharCode.apply(null, new Uint16Array(data)); $('#ciphertext').text(decodedData); } </script>
Related recommendations:
How to get the scroll bar width in js (code example
The above is the detailed content of js code implementation for gzip decompression. For more information, please follow other related articles on the PHP Chinese website!