Comments (15)
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.
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.
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.
Ok, but that's not related to this package anyhow - pako's job is to produce arrays or binary strings.
from pako.
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.
@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.
@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.
@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.
@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.
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.
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.
@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.
@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.
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.
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)
- "Error: -3" when inflating chunks of data HOT 2
- How to decompress blob? HOT 2
- Deflate() creating broken zip file
- Use classes to improve tree-shaking HOT 8
- No way to read Deflate data in chunks. HOT 4
- CI: set minimal permissions on GitHub Workflow HOT 2
- Cannot install pako HOT 1
- library initialization slowing down app start
- TypeError: input.subarray is not a function HOT 1
- Docs update: mention what `inflate` and `deflate` actually return
- Incorrect JSDoc type information for optional parameters
- Expecting a new version release HOT 1
- Different 10th byte gzip header between zlib and pako on Mac OS 13 HOT 1
- License declaration in package.json and LICENSE are not the same
- GZIP pako compressed string results in "Not in GZIP format" in JAVA on decompressing HOT 1
- Inconsistency and `undefined` return in `inflate` method between versions v1 and v2
- Using static dictionary, inflateRaw "Uncaught invalid stored block lengths"
- how to gzip multiple files? HOT 1
- Not compatible with Angular v17
- Getting corrupted data on decompressing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pako.