Git Product home page Git Product logo

Comments (15)

patrickliechty avatar patrickliechty commented on July 20, 2024 3

I had the same issue when trying to send the data to the server from the browser. I fixed it by doing this:

    headers["Content-Encoding"] = "gzip";
    var gzippedJson = gzip.gzip("{test:'hello world'}");
    //If you use charset=x-user-defined-binary it just sends the data through as is.
    //  If you don't do this, it uses utf-8 which adds extra characters and thus is not in gzip format
    var gzippedBLob = new Blob([gzippedJson], {type: "application/json; charset=x-user-defined-binary"});
   xhr.send(gzippedBlob);

from pako.

blacelle avatar blacelle commented on July 20, 2024 1

Hello, I allow myself resurrecting this thread. Here is a Java snippet to handle pako String:

// Convert from pako String format to raw byte[]
byte[] asByteArray = new byte[html.length()];
for (int i = 0; i < html.length(); i++) {
	asByteArray[i] = (byte) html.charAt(i);
}
// Unzip the byte[]
try (GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(asByteArray));
		InputStreamReader osw = new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8)) {
	html = CharStreams.toString(osw);
}

Pako provided the string with:

data.html = pako.gzip(data.html, { to: 'string' });

from pako.

puzrin avatar puzrin commented on July 20, 2024
var output = require('pako').gzip("{test:'hello world'}", { to: 'string' });
console.log(output.charCodeAt(0).toString(16), output.charCodeAt(1).toString(16)); 

1f 8b

It's your bug in file write encoding.

from pako.

puzrin avatar puzrin commented on July 20, 2024

Ok, but that's not related to this package anyhow - pako's job is to produce arrays or binary strings.

from pako.

patrickliechty avatar patrickliechty commented on July 20, 2024

Yes, but it will help those who are trying to use this library. I guess this belongs in a forum an not in the issues.

from pako.

gandhirajan avatar gandhirajan commented on July 20, 2024

@patrickliechty Hi, I m trying out the exact same thing and i m stuck. I m using jquery ajax to post the gzipped string to server but not able to ungzip it on server. Could you brief me the params that needs to be set in AJAX calls to make it work?

from pako.

puzrin avatar puzrin commented on July 20, 2024

@gandhirajan JS strings are UTF16 (2 bytes per char). You should convert it to binary format first (1 byte per element, utf8 for example)

from pako.

gandhirajan avatar gandhirajan commented on July 20, 2024

@puzrin Hi Puzrin, thanks for your response. I tried that but dint work. Do i need to do some encoding while sending the gzipped string from jquery AJAX to server?

from pako.

gandhirajan avatar gandhirajan commented on July 20, 2024

@puzrin We managed to resolve this issue. The issue was with encoding while sending the gzipped string from Javascript to Server. window.btoa() in javascript and base64 decoding at server side resolved the issue

from pako.

puzrin avatar puzrin commented on July 20, 2024

That's not the most effective way, but possible.

I'd suggest to use utf8 instead of base64. Something like this http://ecmanaut.blogspot.ru/2006/07/encoding-decoding-utf8-in-javascript.html (but note that built-in methods can throw exceptions on invalid char sequences).

However i did not compared sizes after different methods use.

from pako.

gandhirajan avatar gandhirajan commented on July 20, 2024

Hi Puzrin, Thanks again for your suggestion. But if i try out the following as mentioned in the link above:
unescape(encodeURIComponent(string)) . I m unable to decode the same in server side as URLDecoder.decode fails due to non ASCII characters.

from pako.

puzrin avatar puzrin commented on July 20, 2024

@gandhirajan I've added working example https://github.com/nodeca/pako/tree/master/examples with both browser and server code. It's not for production but to demonstrate reencoding steps for data flow. See comments.

from pako.

gandhirajan avatar gandhirajan commented on July 20, 2024

@puzrin, Thanks a lot for your response. But was looking for a JAVA sample on server side which i m still figuring out. (Sending gzipped response from server side as AJAX response)

from pako.

dboldureanu avatar dboldureanu commented on July 20, 2024

For me, usefull was patrickliechty's example.
In angular, used this:

`
const gzip = pako.gzip(jsonString);
const blob = new Blob([gzip]);

    const headers = new Headers({
        'Content-Type': 'application/json; charset=x-user-defined-binary',
        'Content-Encoding': 'gzip'
    });

    const reqOptions = new RequestOptions({ headers: headers });

    return this.http.put('URL', blob, reqOptions)
        .map(this.extractJSON)
        .catch((err) => this.httpErrorHandler.handleError(err));

`

from pako.

vras8213 avatar vras8213 commented on July 20, 2024

trying to decompress data of uint8Array getting error . const uncompressedData = inflate(uint8ArrayDe, { raw: true }) how can i fix ? The error is invalid block type

from pako.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.