Home > Web Front-end > JS Tutorial > js code implementation for gzip decompression

js code implementation for gzip decompression

不言
Release: 2018-08-11 16:23:21
Original
3354 people have browsed it

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 = $(&#39;#content&#39;).val();
    str = window.btoa(pako.gzip(str, {to: "string"}))
    $(&#39;#ciphertext&#39;).text(str);
}
function decode(){
    var encodedData = $(&#39;#content&#39;).val();    
    var decodedData = window.atob(encodedData);    
    var charData    = decodedData.split(&#39;&#39;).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));  

    $(&#39;#ciphertext&#39;).text(decodedData);
}
</script>
Copy after login

Related recommendations:

A brief introduction and example analysis of global registration and local registration in vue.js components

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template