JavaScript:解压/膨胀/解压缩/ungzip字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4875020/
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
JavaScript: Decompress / inflate /unzip /ungzip strings
提问by Ondra ?i?ka
I'm looking for JavaScript implementation of string inflating algorithms. I want to compress on the server side (Java), and decompress on the client side (JavaScript).
我正在寻找字符串膨胀算法的 JavaScript 实现。我想在服务器端(Java)压缩,在客户端(JavaScript)解压。
I've found:
我发现:
unzip strings in javascript
That one is marked as answered with an answer for different problem. Other answers are also for something else (unzipping files in ZIP format).
在 javascript 中解压缩字符串
该字符串被标记为回答了不同问题的答案。其他答案也适用于其他问题(以 ZIP 格式解压缩文件)。
JavaScript inflate implementation (possibly FF 3.6 only)
This is closest to what I need. However I'd like to have some alternatives.
JavaScript 膨胀实现(可能仅适用于 FF 3.6)
这最接近我需要的。但是我想有一些选择。
Suggestions?
Thanks, Ondra
建议?
谢谢,昂德拉
Update:I have quite a specific use case, please don't answer "Don't do that in JavaScript." I am writing an "offline" reporting tool (once generated, it's put to a static store) and deflating may save megabytes for a single report. I am constrained by other apps so I can't store it as a zip file.
更新:我有一个非常具体的用例,请不要回答“不要在 JavaScript 中这样做”。我正在编写一个“离线”报告工具(一旦生成,它就会被放到一个静态存储中)并且放气可能会为单个报告节省兆字节。我受到其他应用程序的限制,因此无法将其存储为 zip 文件。
采纳答案by Sean Kinsey
Take a look at this Stack Overflow question, the answers there contains references to multiple compressing engines implemented in javascript. Most of these are based on LZ77.
看看这个堆栈溢出问题,那里的答案包含对在 javascript 中实现的多个压缩引擎的引用。其中大部分基于LZ77。
回答by Gustavo Costa De Oliveira
I don't know how you'd like that, but I like these implementations:
我不知道你喜欢这样,但我喜欢这些实现:
The first is fastest than second, We can usually ensure a fast server, however we don't know the performance of the client machine. Therefore I recommend you choose js-deflate and adjust your java (server side) to inflate.
第一个比第二个快,我们通常可以确保一个快速的服务器,但是我们不知道客户端机器的性能。因此,我建议您选择 js-deflate 并调整您的 java(服务器端)以进行膨胀。
https://github.com/dankogai/js-deflate
https://github.com/dankogai/js-deflate
回答by Redsandro
I created a working example using pako
, modern and fast Zlib
port.
http://jsfiddle.net/9yH7M/2/
我使用pako
现代和快速Zlib
端口创建了一个工作示例。
http://jsfiddle.net/9yH7M/2/
回答by Breton
there's this graphing library that has as part of it, a zlib implementation in javascript. if you scroll down this page a bit, you'll see it as a separate download. http://jsxgraph.uni-bayreuth.de/wp/download/
有一个图形库,其中包含一个 javascript 中的 zlib 实现。如果您稍微向下滚动此页面,您会看到它是一个单独的下载。 http://jsxgraph.uni-bayreuth.de/wp/download/
回答by Cheeso
This example: http://cheeso.members.winisp.net/srcview.aspx?dir=js-unzipshows how you can do ZIP files in Javascript. Now, I know you want ZLIB or DEFLATE compression, rather than ZIP. But, ZIP uses DEFLATE, and within the .js file for that example, there is an InflatingReader class that can INFLATE as it reads.
这个例子:http: //cheeso.members.winisp.net/srcview.aspx?dir=js- unzip展示了如何在 Javascript 中创建 ZIP 文件。现在,我知道您想要 ZLIB 或 DEFLATE 压缩,而不是 ZIP。但是,ZIP 使用 DEFLATE,并且在该示例的 .js 文件中,有一个 InflatingReader 类可以在读取时进行 INFLATE。
The class exposes these methods:
该类公开了这些方法:
readByte()
returns null when EOF is reached, or the value of the byte when successful.
readToEnd()
returns an array of all bytes read, to EOF
beginReadToEnd(callback)
async version of the above
readBytes(n)
returns an array of n bytes read from the source.
beginReadBytes(n, callback)
async version of the above
You can use that code unchanged if you want INFLATE.
如果您想要INFLATE,您可以不变地使用该代码。
If you want ZLIB(aka unzip), then there is a 2-byte signature that you need to read and validate before reading the compressed bytes and doing the INFLATE. Just modify the InflatingReader to read and dump 2 bytes, and it will do ZLIB just fine.
如果您想要ZLIB(又名解压缩),那么在读取压缩字节和执行 INFLATE 之前,您需要读取和验证一个 2 字节的签名。只需修改 InflatingReader 以读取和转储 2 个字节,它就可以很好地执行 ZLIB。
回答by Alexander Staubo
I found a working inflate implementation here:
我在这里找到了一个有效的膨胀实现:
http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt
If you want a slightly cleaner version that namespaces the algorithm, this one should work:
如果你想要一个稍微干净一点的版本来命名算法,这个应该可以工作:
https://github.com/augustl/js-inflate
https://github.com/augustl/js-inflate
Keep in mind that gzipped "inflate" data is prefixed with a two-byte header, and suffixed with a four-byte checksum, which you will need to strip before passing to the algorithm.
请记住,gzip 压缩的“膨胀”数据以两字节标头为前缀,并以四字节校验和为后缀,您需要在传递给算法之前将其去除。
回答by ThiefMaster
Don't do that in JavaScript. It'd be slow and besides that JS doesn't do well with binary data.
不要在 JavaScript 中这样做。它会很慢,而且 JS 不能很好地处理二进制数据。
Simply use gzip transfer encoding on the server side and your browser will take care of decompressing it.
只需在服务器端使用 gzip 传输编码,您的浏览器就会负责解压缩它。