在 JavaScript 中压缩明文?
我有一个简单的类似记事本的 Web 应用程序,我是为了好玩而制作的。当您保存文档时,
我们只是说,我们需要压缩
有 JavaScript 库可以做到这一点吗?首先,纯文本的压缩效果如何?
I have a simple Notepad-like web application I'm making for fun. When you save a document, the contents of a <textarea> are sent to the server via Ajax and persisted in a database.
Let's just say for shits and giggles that we need to compress the contents of the <textarea> before sending it because we're on a 2800 baud modem.
Are there JavaScript libraries to do this? How well does plain text compress in the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您仅使用 7 位 ASCII 字符集,简单的 7 位压缩可能会起作用。谷歌搜索得到了这个:http://www.iamcal.com/png-store/
或者你可以使用LZW
http://rosettacode.org/wiki/LZW_compression#JavaScript
就压缩率而言;根据博士。 Dobbs:
描述任何数据压缩技术的结果都有些困难。所实现的压缩级别差异很大,具体取决于几个因素。当遇到具有任何类型的重复字符串的数据流时,LZW 压缩表现出色。正因为如此,它在压缩英文文本时表现得非常好。预计压缩水平可达 50% 或更高。
Simple 7 bit compression might work if you're only using the 7 bit ascii character set. A google search yielded this: http://www.iamcal.com/png-store/
Or you could use LZW
http://rosettacode.org/wiki/LZW_compression#JavaScript
As far as compression ratio; according to Dr. Dobbs:
It is somewhat difficult to characterize the results of any data compression technique. The level of compression achieved varies quite a bit, depending on several factors. LZW compression excels when confronted with data streams that have any type of repeated strings. Because of this, it does extremely well when compressing English text. Compression levels of 50 percent or better can be expected.
嗯,你不能使用 gzip 压缩。请参阅此处:为什么浏览器无法发送 gzip 请求?
我想你可以去除空白,但这将被证明是不可持续的。我不确定这是否是一种需要抓挠的痒。
我确实通过谷歌搜索找到了这个: http://rumkin.com/tools/compression/compress_huff .php 如果文本足够大,最终将产生一组较小的文本。如果文本很短,它实际上会夸大文本。
我还发现了这个: http://www.sean.co.uk/a /webdesign/javascript_string_compression.shtm
Well, you couldn't use gzip comppression. See here: Why can't browser send gzip request?
I suppose you could strip whitespace, but that would prove unsustainable. I'm not sure if this is an itch that needs scratching.
I did find this with a google search: http://rumkin.com/tools/compression/compress_huff.php That will eventually yield a smaller set of text, if the text is large enough. It actually inflates the text if the text is short.
I also found this: http://www.sean.co.uk/a/webdesign/javascript_string_compression.shtm
首先,运行 LZW 压缩,这会生成二进制格式的压缩数据。
接下来对压缩的二进制数据进行 Base-64 编码。这将生成可以存储在数据库中的压缩数据的文本版本。
要恢复内容,请执行 Base-64 解码。然后LZW解压。
有一些 Java 库可以同时完成这两件事。只需搜索“LZW 压缩 Java”和“base-64 编码 Java”即可。
First, run the LZW compression, this yields compressed data in binary format.
Next then do base-64 encoding on the the compressed binary data. This will yield a text version of the compressed data that you can store in your database.
To restore the contents, do the base-64 decode. Then the LZW decompression.
There are Java libraries to do both. Just search on "LZW compression Java" and on "base-64 encode Java".
它在算法和文本上有很大差异。
我在这里制作自己的压缩算法,截至编写时尚未完成,但它对于英语纯文本压缩已经非常有效。小消息和大消息都压缩约 50%。共享代码片段没有用,因为我正在使用实验性字典压缩,但这是我的项目: https://github.com/j-stodd/SMOL
我还尝试了 Suirtimed 共享的 LZW 压缩,但它似乎表现不佳,它会减少长度但字节基本保持不变。用 LZW 压缩“aaaaaaaa”只会节省一个字节。我的算法将为您节省 5 个字节。
It varies heavily on the algorithm and the text.
I'm making my own compression algorithm here, as of writing its not done but it already works extremely well for English plaintext compression. ~50% compression for both small and large messages. It wouldn't be useful to share a code snippet because I'm using experimental dictionary compression, but heres my project: https://github.com/j-stodd/SMOL
I also tried the LZW compression shared by Suirtimed but it doesn't seem to perform that well, it will decrease length but bytes stay mostly the same. Compressing "aaaaaaaa" with LZW will save you only one byte. My algorithm would save you 5 bytes.