最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to use pako.js javascript? Pako is not defined - Stack Overflow

programmeradmin3浏览0评论

I'm fairly new to JavaScript. I'm currently working on an algorithm that deflates in Java and inflates in javascript. For the most part, I have heard that pako.js is a good tool to use for decompression but I'm having problems implementing it. I created a function in JavaScript that passes the base64 string as a parameter.

function decompressHtml(html){

                var compressedData = atob(html);
                var charData = compressedData.split('').map(function(x){return x.charCodeAt(0);});
                var binData = new Uint8Array(charData);
                var inflated = '';


                try {
                  inflated = pako.inflate(binData);

                } catch (err) {
                  console.log(err);
                }

                return inflated;

        }

It always returns an error that says that pako is not properly defined. Is there a specific script tag/s that need to be inserted in order to define pako? I realise this may be a simple question to answer but I'm not sure of the answer.

I'm fairly new to JavaScript. I'm currently working on an algorithm that deflates in Java and inflates in javascript. For the most part, I have heard that pako.js is a good tool to use for decompression but I'm having problems implementing it. I created a function in JavaScript that passes the base64 string as a parameter.

function decompressHtml(html){

                var compressedData = atob(html);
                var charData = compressedData.split('').map(function(x){return x.charCodeAt(0);});
                var binData = new Uint8Array(charData);
                var inflated = '';


                try {
                  inflated = pako.inflate(binData);

                } catch (err) {
                  console.log(err);
                }

                return inflated;

        }

It always returns an error that says that pako is not properly defined. Is there a specific script tag/s that need to be inserted in order to define pako? I realise this may be a simple question to answer but I'm not sure of the answer.

Share Improve this question asked Aug 10, 2016 at 20:16 atsnamatsnam 3791 gold badge2 silver badges9 bronze badges 3
  • 1 Have you...included pako on the page? – VLAZ Commented Aug 10, 2016 at 20:18
  • I have actually..that's the confusing part – atsnam Commented Aug 12, 2016 at 1:40
  • You can include the pako from CDN jsdelivr.com/package/npm/pako – Sergey Ponomarev Commented Feb 25, 2024 at 14:47
Add a comment  | 

3 Answers 3

Reset to default 12
  1. Download pako.js or pako.min.js from official pako github page
  2. Include the downloaded file in your html as follows:

    <script type="text/javascript" src="pako.js"></script>

Sample code:

var input = "test string";
var output = pako.gzip(input,{ to: 'string' });
alert("compressed gzip string - " + output);
var originalInput = pako.ungzip(output,{ to: 'string' });
alert("uncompressed string - " + originalInput);

This will help you

Decompress

const input = new Uint8Array(res);
const restored = JSON.parse(pako.inflate(input, { to: 'string'}));
console.log(restored)

Compress

const input = new Uint8Array(res);
const output = pako.deflate(input);
console.log(output)

Pako expects to be bound to an object named global, which is (as of Chrome 118) undefined in the browser. You can create the global object and make Pako happy by putting this in a script somewhere before Pako's script tag.

if (!window.global) window.global = window;
发布评论

评论列表(0)

  1. 暂无评论