admin管理员组文章数量:1287613
I was able to depress a string in JavaScript using pako.js
/
// 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 press string and output can be depressed by above code using pako and gzip.
How can I do that?
I was able to depress a string in JavaScript using pako.js
http://jsfiddle/9yH7M/1/
// 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 press string and output can be depressed by above code using pako and gzip.
How can I do that?
Share Improve this question edited Jun 4, 2018 at 13:52 krlzlx 5,82214 gold badges49 silver badges59 bronze badges asked Jun 4, 2018 at 13:16 sireeshasireesha 1191 gold badge1 silver badge6 bronze badges 3- What's going wrong when you simply do what's written in the docs? Note that pako has options to press/depress to/from string, so your typed array is not necessary. – ASDFGerte Commented Jun 4, 2018 at 13:20
- 1 @ASDFGerte I am doing var str ="Hello world"; var test = pako.gzip(new Uint8Array(str), {to: 'string'}); alert(btoa(test)); I get same output " H4sIAAAAAAAAAwMAAAAAAAAAAAA= " for any string and also not able to get back the original string after depressing the output. Where am I going wrong? – sireesha Commented Jun 5, 2018 at 5:32
-
The way you construct your
Uint8Array
is not working (always empty) - debugging your test for a short moment would quickly tell you that. You could fix that, but way easier is noticing that ingzip(data[, options])
, data has a described type of "Uint8Array | Array | String", which means you can just dolet test = pako.gzip(str, {to: 'string'});
. Reverting the process is as simple aspako.ungzip(test, { to: 'string' });
. – ASDFGerte Commented Jun 5, 2018 at 8:06
2 Answers
Reset to default 6Compression:
let pressed_str = pako.gzip(str, {to: 'string'});
Depression:
let original_str = pako.ungzip(pressed_str, { to: 'string' });
There is a duplicate question, so I'm sharing my answer here.
https://stackoverflow./a/75934779/7295761
{to: 'string'}
simply doesn't work for latest pako.js
本文标签: Gzip a string in javascript using pakojsStack Overflow
版权声明:本文标题:Gzip a string in javascript using pako.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741240943a2363963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论