9

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.

atsnam
  • 369
  • 1
  • 2
  • 8

2 Answers2

11
  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);
Gandhi
  • 11,875
  • 4
  • 39
  • 63
3

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)
perumal N
  • 641
  • 2
  • 10
  • 27