javascript - Compressed file in jszip is bigger than uncompressed content - Stack Overflow

Can somebody explain, how is it possible, that jszip is not pressing files? I am trying to use pression

Can somebody explain, how is it possible, that jszip is not pressing files? I am trying to use pression and the pressed zip file size is bigger than file size of unpressed files inside. Am I doing something wrong?

var fs = require("fs");
var JSZip = require("jszip");

var zip = new JSZip();
zip.file('try.txt', 'Hello World ');

zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('out.zip')).on('finish', function () {
    // JSZip generates a readable stream with a "end" event,
    // but is piped here in a writable stream which emits a "finish" event.
    console.log("out.zip written.");
});

I see the issue also on the official page when downloading example. /

As I can see, the file is the same usually as unpressed file and it is doing no pression at all.

Can somebody explain, how is it possible, that jszip is not pressing files? I am trying to use pression and the pressed zip file size is bigger than file size of unpressed files inside. Am I doing something wrong?

var fs = require("fs");
var JSZip = require("jszip");

var zip = new JSZip();
zip.file('try.txt', 'Hello World ');

zip.generateNodeStream({type:'nodebuffer',streamFiles:true}).pipe(fs.createWriteStream('out.zip')).on('finish', function () {
    // JSZip generates a readable stream with a "end" event,
    // but is piped here in a writable stream which emits a "finish" event.
    console.log("out.zip written.");
});

I see the issue also on the official page when downloading example. https://stuk.github.io/jszip/

As I can see, the file is the same usually as unpressed file and it is doing no pression at all.

Share Improve this question asked Sep 10, 2019 at 7:44 Pavol TravnikPavol Travnik 1,0733 gold badges17 silver badges35 bronze badges 2
  • Try an example with a larger and more obviously press-able value like '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111' or maybe 20x as many as that and see if that results in a smaller value. zip has some overhead that the unpressed file won't, and it's quite probable that the "pressed" file will actually be larger for very short examples – Patrick Fay Commented Sep 10, 2019 at 7:51
  • I did, it doesn't work either, it returns zip file with same size as depressed file inside. I have to be missing something (most probably documentation does). – Pavol Travnik Commented Sep 10, 2019 at 7:55
Add a ment  | 

1 Answer 1

Reset to default 9

Ok, I took a look at the example on their website as you suggested. I made an obviously pressable file like I suggested in my ment (and you followed up on), download a 100KB file, obviously unpressed. I then unzipped that and rezipped it using both windows and 7zip and resulted in a ~1KB file instead. You're definitely right, jszip's own example is creating an unpressed zip.

For the case on their website, generate_async() doesn't press by default, you have to pass pression options like so, and adjusting the call on their website does work, like so:

var zip = new JSZip();
zip.file("Hello.txt", "11111111 I had a bunch more 1s that I removed\n");
var img = zip.folder("images");
img.file("smile.gif", imgData, {base64: true});
zip.generateAsync({type:"blob",
    /* NOTE THESE ADDED COMPRESSION OPTIONS */
    /* deflate is the name of the pression algorithm used */
    pression: "DEFLATE",
    pressionOptions: {
         /* pression level ranges from 1 (best speed) to 9 (best pression) */
        level: 9
    }})
.then(function(content) {
    // see FileSaver.js
    saveAs(content, "example.zip");
});

Similarly, for your call I believe adding similar options should resolve it based on the generateNodeStream() documentation:

zip.generateNodeStream(
    {type:'nodebuffer',streamFiles:true,pression: "DEFLATE", pressionOptions: {level: 9}}
).pipe(fs.createWriteStream('out.zip')).on('finish', function () {
    // JSZip generates a readable stream with a "end" event,
    // but is piped here in a writable stream which emits a "finish" event.
    console.log("out.zip written.");
});

Also, you're not the first to have this issue. Here's another user on their github who fell into the same trap: https://github./Stuk/jszip/issues/503 I mented on that issue suggesting they change the examples to default to using pression to help avoid this problem. I didn't create a separate issue for it, but if you were passionate about it you could do so.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302605a4418310.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信