在 JavaScript 中壓縮純文本? (Compressing plaintext in JavaScript?)


問題描述

在 JavaScript 中壓縮純文本? (Compressing plaintext in 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?

‑‑‑‑‑

參考解法

方法 1:

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.    

方法 2:

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

方法 3:

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".

方法 4:

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.

(by AgileMeansDoAsLittleAsPossibleDemitrius NelonStephenDan8080Jstodd)

參考文件

  1. Compressing plaintext in JavaScript? (CC BY‑SA 3.0/4.0)

#compression #javascript






相關問題

CSS 壓縮和組合 / js 縮小 - 在運行時或構建時更好? (CSS compression & combining / js minification - Better to do at runtime or at build time?)

在javascript中壓縮包含音頻PCM數據的blob (compress blob containing audio PCM data in javascript)

如何提高@font-face 的加載性能 (How to improve loading performance of @font-face)

ServiceStack 流壓縮 (ServiceStack Stream Compression)

有沒有辦法讀取壓縮存檔的屬性? (Is there any way to read the properties of a Compressed Archive?)

德爾福 2009 中的 Zlib (Zlib in Delphi 2009)

SQL 2008開啟頁面壓縮後如何回收空間? (How to reclaim space after turning on page compression in SQL 2008?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 JavaScript 中壓縮純文本? (Compressing plaintext in JavaScript?)

如何遞歸壓縮文件夾? (How to zip a folder recursively?)

使用 GZIPOutputStream 壓縮字符串 (Compressing a string using GZIPOutputStream)

壓縮後 1 KB 可以容納多少文本? (How much text I can fit in 1 kilobyte with compression?)







留言討論