发布网友 发布时间:2022-04-22 15:44
共2个回答
懂视网 时间:2022-04-22 20:05
本篇文章给大家带来的内容是关于js实现gzip解压缩的代码实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
代码如下:
<!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>
热心网友 时间:2022-04-22 17:13
其实php对gzip解压很简单,用内置的gzdecode函数就可以了,不过很可惜我配置了半天也无法支持gzdecode函数,所以只好变通一下: 复制代码 代码如下: if (!function_exists('gzdecode')) { function gzdecode ($data) { $flags = ord(substr($data, 3, 1)); $headerlen = 10; $extralen = 0; $filenamelen = 0; if ($flags & 4) { $extralen = unpack('v' ,substr($data, 10, 2)); $extralen = $extralen[1]; $headerlen += 2 + $extralen; } if ($flags & 8) // Filename $headerlen = strpos($data, chr(0), $headerlen) + 1; if ($flags & 16) // Comment $headerlen = strpos($data, chr(0), $headerlen) + 1; if ($flags & 2) // CRC at end of file $headerlen += 2; $unpacked = @gzinflate(substr($data, $headerlen)); if ($unpacked === FALSE) $unpacked = $data; return $unpacked; } } 调用方法很简单: 复制代码 代码如下: $f=@file_get_contents(""); echo gzdecode($f);