Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pako.js javascript? Pako is not defined

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.

like image 898
atsnam Avatar asked Aug 10 '16 20:08

atsnam


2 Answers

  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);
like image 119
Gandhi Avatar answered Nov 10 '22 03:11

Gandhi


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)
like image 2
perumal N Avatar answered Nov 10 '22 05:11

perumal N