本篇文章給大家?guī)淼膬?nèi)容是關(guān)于js實(shí)現(xiàn)gzip解壓縮的代碼實(shí)現(xiàn),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。
代碼如下:
<!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>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com