javascript - 使用zip.js压缩Blob [英] javascript - zip a Blob with zip.js

查看:3578
本文介绍了javascript - 使用zip.js压缩Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在WebSQl中存储大量文本,因此我决定使用 zip.js
并存储压缩的Blob

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

从文档中您可以压缩blob如下

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 ?

推荐答案

在这里检查这个答案 - 它不需要一个文件名,我敢打赌它更容易使用。我已经尝试了相当多的javascript压缩/解压缩实现,并被困扰的问题,如原始数据的大小,总速度,效率等的限制。在JavaScript中找到一个好的压缩/解压缩实现是奇怪的,但是幸运的是,这没有失败我(我已经使用了很多):

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):

在javascript 中压缩blob

Compressing a blob in javascript

您目前的实现需要文件名,因为它试图与zip一致,以便您可以将其保存到例如桌面,并用您最喜欢的zip实用程序打开它。听起来你的挑战与我非常相似,我需要保存和恢复压缩的项目从本地存储在浏览器以及在服务器上。

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.

这篇关于javascript - 使用zip.js压缩Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆