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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With