file - javascript - zip a Blob with zip.js - Stack Overflow

I need to store a lot of text in WebSQl, so I decided to press the text with zip.jsand store the press

I need to store a lot of text in WebSQl, so I decided to press the text with zip.js and store the pressed Blobs.

From the documentation you can press a blob as follows:

function zipBlob(filename, blob, callback) {
   // use a zip.BlobWriter object to write zipped data into a Blob object
   zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
      // use a BlobReader object to read the data stored into blob variable
      zipWriter.add(filename, new zip.BlobReader(blob), function() {
         // close the writer and calls callback function
         zipWriter.close(callback);
      });
   }, onerror);
}

Although this works, I don't understand why you need to specify a filename. Is this really necessary? And, is this file always removed after pression?

I need to store a lot of text in WebSQl, so I decided to press the text with zip.js and store the pressed Blobs.

From the documentation you can press a blob as follows:

function zipBlob(filename, blob, callback) {
   // use a zip.BlobWriter object to write zipped data into a Blob object
   zip.createWriter(new zip.BlobWriter("application/zip"), function(zipWriter) {
      // use a BlobReader object to read the data stored into blob variable
      zipWriter.add(filename, new zip.BlobReader(blob), function() {
         // close the writer and calls callback function
         zipWriter.close(callback);
      });
   }, onerror);
}

Although this works, I don't understand why you need to specify a filename. Is this really necessary? And, is this file always removed after pression?

Share Improve this question edited May 24, 2022 at 1:44 Henry Ecker 35.7k19 gold badges47 silver badges64 bronze badges asked Mar 7, 2013 at 15:28 Jeanluca ScaljeriJeanluca Scaljeri 29.2k66 gold badges235 silver badges382 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Check this answer here - it does not require a filename, and I'll bet its much easier to use. I've tried quite a few javascript pression/depression implementations and have been stung by problems such as limits on size of original data, overall speed, efficiency, and so forth. It is oddly difficult to find a good pression/depression implementation in javascript, but thankfully this one hasn't failed me yet (and I've used it quite a bit):

Compressing a blob in javascript

The implementation you have currently requires the filename because it is trying to be consistent with the zip, so that you can save it for example to a desktop and open it with your favorite zip utility. It sounds like your challenge is very similar to mine, I needed to save and restore pressed items out of local storage in the browser as well as on the server.

The filename is necessary according to this implementation. It wouldn't be necessary if you were only pressing the data, but zip.js builds zip files, which store files, which must have filenames.

In your original example, zipWriter.add() effectively converts your blob into a new file and adds it to a zip – and the "filename" parameter is the name you want that new file to have.

Here's an example that uses zip.js to add multiple blobs to a zip and then downloads it with FileSaver.js:

function zipBlob() {
    zip.createWriter(new zip.BlobWriter("application/zip"), function(writer) {
        files = ["abcd", "123"];
        var f = 0;

        function nextFile(f) {
            fblob = new Blob([files[f]], { type: "text/plain" });
            writer.add("file"+f, new zip.BlobReader(fblob), function() {
                // callback
                f++;
                if (f < files.length) {
                    nextFile(f);
                } else close();
            });
        }

        function close() {
            // close the writer
            writer.close(function(blob) {
                // save with FileSaver.js
                saveAs(blob, "example.zip");
            });
        }

        nextFile(f);

    }, onerror);
}

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

相关推荐

  • file - javascript - zip a Blob with zip.js - Stack Overflow

    I need to store a lot of text in WebSQl, so I decided to press the text with zip.jsand store the press

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信