javascript 客户端数据压缩/解压缩?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16275512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Client side data compress/decompress?
提问by simo
I'm looking for a JavaScript implementation of a string compress/decompress algorithm where data is created at the client side and stored in hidden fields within HTML forms.
我正在寻找字符串压缩/解压缩算法的 JavaScript 实现,其中数据在客户端创建并存储在 HTML 表单中的隐藏字段中。
I read about gzip, but it compresses the data server side whereas in my case I want to compress it client side, send it to the server, or receive it from server to decompress it again at client side.
我阅读了gzip,但它压缩了数据服务器端,而在我的情况下,我想将其压缩到客户端,将其发送到服务器,或从服务器接收它以在客户端再次解压缩。
I found this LZF Compressionexample based on LZFjsbut it will generate binary data which needs to be processed and stored in a hidden form field, and it works on files rather than pure data.
我发现了这个基于LZFjs 的LZF 压缩示例,但它会生成需要处理和存储在隐藏表单字段中的二进制数据,并且它适用于文件而不是纯数据。
Suggestions on a pure data client-side data compression/decompression that is also efficient?
关于纯数据客户端数据压缩/解压缩也很有效的建议?
回答by Cyan
There is this open-source Javascript compression library, by Pierre curto : https://github.com/pierrec/node-lz4
Pierre curto 有这个开源 Javascript 压缩库:https: //github.com/pierrec/node-lz4
Googling around, I also found this zlib implementation (not tested by me) : http://nodejs.org/api/zlib.html
谷歌搜索,我还发现了这个 zlib 实现(未经我测试):http: //nodejs.org/api/zlib.html
回答by Mrchief
You may also try JSZip. To run it in browser you just have to downloadand include dist/jszip.js or dist/jszip.min.js.
你也可以试试JSZip。要在浏览器中运行它,您只需下载并包含 dist/jszip.js 或 dist/jszip.min.js。
This is actively supported and supports a wide variety of browsers including everyone's favorite IE6/7/8!
这是积极支持的,支持各种浏览器,包括大家最喜欢的IE6/7/8!
Usage (from their docs):
用法(来自他们的文档):
var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); var img = zip.folder("images"); img.file("smile.gif", imgData, {base64: true}); var content = zip.generate({type:"blob"});
var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); var img = zip.folder("images"); img.file("smile.gif", imgData, {base64: true}); var content = zip.generate({type:"blob"});