1

I read a topic about decompress a string in javascript using pako.js
ZLIB Decompression - Client Side
http://jsfiddle.net/9yH7M/1/
This is code to decompress

// Get some base64 encoded binary data from the server. Imagine we got this:
var b64Data     = 'H4sIAAAAAAAAAwXB2w0AEBAEwFbWl2Y0IW4jQmziPNo3k6TuGK0Tj/ESVRs6yzkuHRnGIqPB92qzhg8yp62UMAAAAA==';

// Decode base64 (convert ascii to binary)
var strData     = atob(b64Data);

// Convert binary string to character-number array
var charData    = strData.split('').map(function(x){return x.charCodeAt(0);});

// Turn number array into byte-array
var binData     = new Uint8Array(charData);

// Pako magic
var data        = pako.inflate(binData);

// Convert gunzipped byteArray back to ascii string:
var strData     = String.fromCharCode.apply(null, new Uint16Array(data));

// Output to console
console.log(strData);

I want a method to compress string and ouput can be decompress by above code
How can do that

Community
  • 1
  • 1
Nguyen Anh Duc
  • 45
  • 1
  • 1
  • 12
  • http://stackoverflow.com/questions/14620769/decompress-gzip-and-zlib-string-in-javascript ? Inflate means decompress, Deflate means compress, ... https://nodeca.github.io/pako/#Deflate has example too ? – Darryl Miles Nov 11 '15 at 04:28
  • Your topic is mention topic in my question. It's not have compress string – Nguyen Anh Duc Nov 11 '15 at 04:31
  • The only matter missing in the example is to conversion of the binary output data (from `Deflate.result`) into BASE64 but there is `btoa()` right ? https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding – Darryl Miles Nov 11 '15 at 04:35
  • var str = "Nguyen ANh Duc"; var data = pako.deflate(str); alert(btoa(data)); – Nguyen Anh Duc Nov 11 '15 at 06:36
  • this is my code but can not get right result. sorry i'm newbie – Nguyen Anh Duc Nov 11 '15 at 06:37
  • Oh, i fix this issue, i found error in pako.js, fix it and every thing is OK, thanks you man – Nguyen Anh Duc Nov 11 '15 at 06:50

3 Answers3

1
  • The code to decompress from b64Data can be simplified:
return pako.inflate(atob(b64Data), { to: 'string' });
  • The code to compress to b64Data:
return btoa(pako.deflate(stringToCompress, { to: 'string' }));
0

You can do following:

var binData = pako.deflate( "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
.map(function(x){return x.charCodeAt(0);}));

yields:

binData : Uint8Array(37) [120, 156, 115, 116, 114, 118, 113, 117, 115, 247, 240, 244, 242, 246, 241, 245, 243, 15, 8, 12, 10, 14, 9, 13, 11, 143, 136, 140, 114, 196, 41, 3, 0, 150, 1, 15, 191]

var strData = String.fromCharCode.apply(null, pako.inflate(String.fromCharCode.apply(null, binData).split("").  map(function(x){return x.charCodeAt(0);})));

//yields back 

"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"

TylerH
  • 20,799
  • 66
  • 75
  • 101
Suas
  • 1
  • 1
  • 1
    While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – Sneftel Aug 22 '19 at 13:32
0

After wasting 2 hours trying more than 20 answers on different site, I figured out the how to gzip & base64. It's so simple:

var your_input_string = "hello hello hello!";
var compressed_uint8array = pako.gzip(your_input_string);
var b64encoded_string = btoa(String.fromCharCode.apply(null, compressed_uint8array));
console.log(b64encoded_string);

You need to include pako.js. Refer to this answer to learn how to get it: How to use pako.js javascript? Pako is not defined

recolic
  • 554
  • 4
  • 18
  • When using your code (with typescript), I get the following error : `Argument of type 'Uint8Array' is not assignable to parameter of type 'number[]'`. `fromCharCode()` is expecting `number[]` type as input parameter. Any ideas how to solve this ? – Beinje Jul 12 '23 at 07:45
  • 1
    For anyone looking for a solution to the typescript error above, I changed the third line to `btoa(String.fromCharCode(...compressed_uint8array))`. Source : https://stackoverflow.com/questions/75961365/argument-of-type-uint8array-is-not-assignable-to-parameter-of-type-number – Beinje Jul 12 '23 at 08:32