Search code examples
javascriptfilecompressionzipblob

javascript - zip a Blob with zip.js


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

From the documentation you can compress 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 compression?


Solution

  • 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 compression/decompression 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 compression/decompression 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 compressed items out of local storage in the browser as well as on the server.