211

我正在编写一个 Web 应用程序,它需要通过 AJAX 将 JSON 数据存储在一个小的、固定大小的服务器端缓存中(想想:Opensocial quotas)。我无法控制服务器。

我需要减少存储数据的大小以保持在服务器端配额内,并希望能够在浏览器中对字符串化的 JSON 进行 gzip 压缩,然后再将其发送到服务器。

但是,我在 Gzip 的 JavaScript 实现方式中找不到太多。关于在发送之前如何在客户端压缩数据的任何建议?

4

9 回答 9

145

Edit There appears to be a better LZW solution that handles Unicode strings correctly at http://pieroxy.net/blog/pages/lz-string/index.html (Thanks to pieroxy in the comments).


I don't know of any gzip implementations, but the jsolait library (the site seems to have gone away) has functions for LZW compression/decompression. The code is covered under the LGPL.

// LZW-compress a string
function lzw_encode(s) {
    var dict = {};
    var data = (s + "").split("");
    var out = [];
    var currChar;
    var phrase = data[0];
    var code = 256;
    for (var i=1; i<data.length; i++) {
        currChar=data[i];
        if (dict[phrase + currChar] != null) {
            phrase += currChar;
        }
        else {
            out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
            dict[phrase + currChar] = code;
            code++;
            phrase=currChar;
        }
    }
    out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
    for (var i=0; i<out.length; i++) {
        out[i] = String.fromCharCode(out[i]);
    }
    return out.join("");
}

// Decompress an LZW-encoded string
function lzw_decode(s) {
    var dict = {};
    var data = (s + "").split("");
    var currChar = data[0];
    var oldPhrase = currChar;
    var out = [currChar];
    var code = 256;
    var phrase;
    for (var i=1; i<data.length; i++) {
        var currCode = data[i].charCodeAt(0);
        if (currCode < 256) {
            phrase = data[i];
        }
        else {
           phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
        }
        out.push(phrase);
        currChar = phrase.charAt(0);
        dict[code] = oldPhrase + currChar;
        code++;
        oldPhrase = phrase;
    }
    return out.join("");
}
于 2008-11-16T21:29:12.967 回答
59

我遇到了另一个问题,我不想在 gzip 中编码数据,而是要解码 gzipped data。我在浏览器之外运行 javascript 代码,所以我需要使用javascript 对其进行解码。

我花了一些时间,但我发现在JSXGraph库中有一种读取 gzip 压缩数据的方法。

这是我找到图书馆的地方:http: //jsxgraph.uni-bayreuth.de/wp/2009/09/29/jsxcompressor-zlib-compressed-javascript-code/ 甚至还有一个独立的实用程序可以做到这一点,JSXCompressor,并且代码已获得 LGPL 许可。

只需在项目中包含 jsxcompressor.js 文件,然后您就可以读取 base 64 编码的 gzip 数据:

<!doctype html>
</head>
<title>Test gzip decompression page</title>
<script src="jsxcompressor.js"></script>
</head>
<body>
<script>
    document.write(JXG.decompress('<?php 
        echo base64_encode(gzencode("Try not. Do, or do not. There is no try.")); 
    ?>'));
</script>
</html>

我知道这不是您想要的,但我仍然在这里回复,因为我怀疑它会对某些人有所帮助。

于 2011-04-12T09:32:58.747 回答
41

我们刚刚发布了 pako https://github.com/nodeca/pako,zlib到 javascript 的端口。我认为这是现在deflate / inflate / gzip / ungzip最快的js实现。此外,它拥有民主的麻省理工学院执照。Pako 支持所有 zlib 选项,其结果是二进制相等的。

例子:

var inflate = require('pako/lib/inflate').inflate; 
var text = inflate(zipped, {to: 'string'});
于 2014-03-15T19:41:59.673 回答
17

我将 LZMA 的实现从 GWT 模块移植到独立的 JavaScript 中。它被称为LZMA-JS

于 2010-09-06T16:14:22.020 回答
14

以下是用 Javascript 实现的其他一些压缩算法:

于 2009-09-15T17:13:16.160 回答
8

我没有测试,但是有一个 ZIP 的 javascript 实现,称为 JSZip:

https://stuk.github.io/jszip/

于 2010-08-16T14:51:45.977 回答
0

I guess a generic client-side JavaScript compression implementation would be a very expensive operation in terms of processing time as opposed to transfer time of a few more HTTP packets with uncompressed payload.

Have you done any testing that would give you an idea how much time there is to save? I mean, bandwidth savings can't be what you're after, or can it?

于 2008-11-16T20:34:29.097 回答
-4

大多数浏览器都可以即时解压缩 gzip。这可能是比 javascript 实现更好的选择。

于 2008-11-16T20:04:06.663 回答
-5

You can use a 1 pixel per 1 pixel Java applet embedded in the page and use that for compression.

It's not JavaScript and the clients will need a Java runtime but it will do what you need.

于 2008-11-16T20:51:36.007 回答