nodeca / pako

high speed zlib port to javascript, works in browser & node.js

Home Page:http://nodeca.github.io/pako/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using pako.gzip within a Browser/Javascript app for compression that matches Serverside PHP decompression

deehill opened this issue · comments

Hi.

I'm creating a test Javascript/Browser app that compresses a test string. the compression happens via pako.gzip.

In order for the test to succeed, the data needs to "match" the php code. It doesn't.

////////////////////////////////////////////
var tStr="this is a short test";

a2222=encodeURI(tStr);

//-- produces this%20is%20a%20short%20test%20message

//--convert Javascript obj to "string"
a222=JSON.stringify(a2222);

//-- produces ""this%20is%20a%20short%20test%20message""

/*
got the encoded string.. now gzip the str
to gen a binArray
*/

a3g1=pako.gzip(a222, {to: "string"});

//--this gets the following array
int8Array(43) [31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 83, 42, 201, 200, 44, 86, 53, 50, 0, 19, 137, 64, 92, 156, 145, 95, 84, 2, 164, 75, 82, 139, 75, 148, 0, 42, 19, 25, 183, 30, 0, 0, 0, buffer: ArrayBuffer(43), byteLength: 43, byteOffset: 0, length: 43]
/////////////////////////////////////////////////////////////////////

At this point, I wanted to make sure I could replicate this same basic process on the ServerSide in the test PHP app. I failed, miserably!

The test php code segment
////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////

I'm trying to figure out how to get the output of the pako.gzip() to match the output of the php $a22=gzdeflate($a222)

The arrays are "similar" but not a match.

Other users have tried to accomplish similar tests with no success.

One user suggested implementing a "Buffer" to resolve the issue, and discussed the difference between UTF-8 and UTF-16 between Browser/Javascript and php.

.
const compress = str => Buffer.from(pako.deflateRaw(str)).toString('base64');
console.log(compress('asdfasdfasdfasdf')); //SyxOSUtEwgA=
.
However, while I can find a nodejs safe-buffer module, I have no clue as to how to invoke/init the "Buffer" within the test case to run.

Any thoughts/comments you can share would be great!

thanks

There is small example https://github.com/nodeca/pako/tree/master/examples, but in general, questions about sending/receiving data is out of scope.