Nguyen Anh Duc
Nguyen Anh Duc

Reputation: 45

Gzip string in javascript using pako.js

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

Upvotes: 1

Views: 14061

Answers (4)

Remco Van Der Velde
Remco Van Der Velde

Reputation: 41

i did this in php:

echo 'id: ' . $id . "\n";
echo 'event: ping' . "\n";
 echo 'data:' . base64_encode(gzdeflate(json_encode(['time' => microtime(true), 'date' => date('Y-m-d- H:i:s')]), 9)) . "\n";
echo "\n\n";

as a server sent event with a compressed json data

and this in javascript to:

const eventSource = new EventSource(sse.url,{
        withCredentials: true,
});
eventSource.addEventListener('ping', (event) => {
        let data = pako.inflate(atob(event.data),{ raw: true, to: 'string' });
        console.log(data);
        console.log('waiting for response');
    });

Upvotes: 0

recolic
recolic

Reputation: 626

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

Upvotes: 4

Sebastien Chandonnet
Sebastien Chandonnet

Reputation: 11

  • 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' }));

Upvotes: 1

Suas
Suas

Reputation: 1

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"

Upvotes: 0

Related Questions