ZLIB解压缩 - 客户端

时间:2010-12-22 08:14:53

标签: javascript zlib compression

我正在接收数据作为“ ZLIB ”压缩输入流。

使用Javascript / Ajax / JQuery,我需要在客户端解压缩它。

有没有办法这样做?请帮忙。

我已经在JAVA中使用了这个,如下所示,但需要在客户端执行此操作。

url = new URL(getCodeBase(), dataSrcfile); 
URLConnection urlConn = url.openConnection();
urlConn.setUseCaches(false); 
InputStream in = urlConn.getInputStream();
InflaterInputStream inflate = new InflaterInputStream(in);
InputStreamReader inputStreamReader = new InputStreamReader(inflate);
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
// Read until no more '#'
int i = 0;
int nHidden = 0;
String line1;
do //------------------------Parsing Starts Here
{
    line1 = bufReader.readLine();
.............
...... so on

8 个答案:

答案 0 :(得分:17)

Pako是一个完整且现代的Zlib端口。

这是一个非常简单的例子,你可以在那里工作。

获取pako.js,您可以像这样解压缩byteArray:

<html>
<head>
  <title>Gunzipping binary gzipped string</title>
  <script type="text/javascript" src="pako.js"></script>
  <script type="text/javascript">

    // Get datastream as Array, for example:
    var charData    = [31,139,8,0,0,0,0,0,0,3,5,193,219,13,0,16,16,4,192,86,214,151,102,52,33,110,35,66,108,226,60,218,55,147,164,238,24,173,19,143,241,18,85,27,58,203,57,46,29,25,198,34,163,193,247,106,179,134,15,50,167,173,148,48,0,0,0];

    // Turn number array into byte-array
    var binData     = new Uint8Array(charData);

    // Pako magic
    var data        = pako.inflate(binData);

    // Convert gunzipped byteArray back to ascii string:
    var strData     = String.fromCharCode.apply(null, new Uint16Array(data));

    // Output to console
    console.log(strData);

  </script>
</head>
<body>
    Open up the developer console.
</body>
</html>

正在运行示例:http://jsfiddle.net/9yH7M/

或者,您可以在发送之前对数组进行base64编码,因为当以JSON或XML格式发送时,Array会占用大量开销。同样解码:

// Get some base64 encoded binary data from the server. Imagine we got this:
var b64Data     = 'H4sIAAAAAAAAAwXB2w0AEBAEwFbWl2Y0IW4jQmziPNo3k6TuGK0Tj/ESVRs6yzkuHRnGIqPB92qzhg8yp62UMAAAAA==';

// Decode base64 (convert ascii to binary)
var strData     = atob(b64Data);

// Convert binary string to character-number array
var charData    = strData.split('').map(function(x){return x.charCodeAt(0);});

// Turn number array into byte-array
var binData     = new Uint8Array(charData);

// Pako magic
var data        = pako.inflate(binData);

// Convert gunzipped byteArray back to ascii string:
var strData     = String.fromCharCode.apply(null, new Uint16Array(data));

// Output to console
console.log(strData);

正在运行示例:http://jsfiddle.net/9yH7M/1/

为了更高级,这里是pako API documentation

答案 1 :(得分:11)

最新的产品是https://github.com/imaya/zlib.js

我认为它比替代品要好得多。

答案 2 :(得分:3)

我们的库JSXGraph包含deflate,unzip和gunzip算法。请查看源代码中的jsxcompressor(JSXGraph的衍生产品,请参阅http://jsxgraph.uni-bayreuth.de/wp/download/)或Utils.js。

答案 3 :(得分:2)

就像您对问题的第一条评论所说,我怀疑您确实希望浏览器处理解压缩。如果我弄错了,您可能想查看JSXGraph库,它应该包含纯JS implementations for deflate and unzip

答案 4 :(得分:1)

dankogai的js-deflate项目可能正是您所寻找的。我还没有尝试过,但rawinflate.js代码似乎相当小,应该能够解压缩DEFLATE / zlib:ed数据。

答案 5 :(得分:1)

尝试pako https://github.com/nodeca/pako,它不仅仅是膨胀/收缩,而是精确的zlib端口到javascript,几乎支持所有功能和选项。此外,它是现代浏览器中最快的实现。

答案 6 :(得分:0)

你应该从zlib rfc

看到zlib rfc

我测试的javascript inflate代码:inflate in Javascript 我写的java代码:

    static public byte[] compress(byte[] input) {
    Deflater deflater = new Deflater();
    deflater.setInput(input, 0, input.length);
    deflater.finish();
    byte[] buff = new byte[input.length + 50];
    deflater.deflate(buff);

    int compressedSize = deflater.getTotalOut();

    if (deflater.getTotalIn() != input.length)
        return null;

    byte[] output = new byte[compressedSize - 6];

    System.arraycopy(buff, 2, output, 0, compressedSize - 6);// del head and
                                                                // foot byte
    return output;
}

非常重要的是在Java中的deflate你必须削减头2字节,脚4字节,以获得原始的deflate。

答案 7 :(得分:0)

Browserify-zlib对我来说很完美,它使用pako并且与zlib具有完全相同的api。在我用pako在客户端压缩/解压缩zlib编码的有效负载几天后,我可以说browserify-zlib非常方便。