Javascript ZLIB 解压 - 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4507316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:41:38  来源:igfitidea点击:

ZLIB Decompression - Client Side

javascriptzlibcompression

提问by Rajeev Raina

I am receiving data as an "ZLIB" compressed inputstream.

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

Using Javascript/Ajax/JQuery, I need to uncompress it on the client side.

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

Is there a way to do so? Please help.

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

I already have this working in JAVA as below, but need to do this on Client Side.

我已经在 J​​AVA 中进行了如下操作,但需要在客户端执行此操作。

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

回答by Redsandro

Pakois a full and modern Zlibport.

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

Here is a very simple example and you can work from there.

这是一个非常简单的示例,您可以从那里开始工作。

Get pako.jsand you can decompress byteArray like so:

获取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>

Running example: http://jsfiddle.net/9yH7M/

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

Alternatively you can base64 encode the array before you send it over as the Array takes up a lot of overhead when sending as JSON or XML. Decode likewise:

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

// 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);

Running example: http://jsfiddle.net/9yH7M/1/

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

To go more advanced, here is the pakoAPI documentation.

为了更高级,这里是pakoAPI 文档

回答by user1009908

A more recent offering is https://github.com/imaya/zlib.js

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

I think it's much better than the alternatives.

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

回答by Alfred Wassermann

Our library JSXGraph contains the deflate, unzip and gunzip algorithm. Please, have a look at jsxcompressor (a spin-off from JSXGraph, see http://jsxgraph.uni-bayreuth.de/wp/download/) or at Utils.js in the source code.

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

回答by cg.

Just as the first comments to your question suggest, I would suspect that you actually want the browser to handle the decompression. If I am mistaken, you might want to check out the JSXGraphlibrary, it is supposed to contain pure JS implementations for deflate and unzip.

正如对您的问题的第一条评论所暗示的那样,我怀疑您实际上希望浏览器处理解压。如果我弄错了,您可能想查看JSXGraph库,它应该包含用于 deflate 和 unzip 的JS 实现

回答by Vitaly

Try pako https://github.com/nodeca/pako, it's not just inflate/deflate, but exact zlib port to javascript, with almost all features and options supported. Also, it's the fastest implementation in modern browsers.

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

回答by marcus256

The js-deflateproject by dankogai may be what you are looking for. I haven't actually tried it, but the rawinflate.js code seems fairly minimal, and should be able to decompress DEFLATE/zlib:ed data.

dankogai的js-deflate项目可能就是你要找的。我还没有真正尝试过,但是 rawinflate.js 代码看起来相当少,应该能够解压缩 DEFLATE/zlib:ed 数据。

回答by tanktg

Browserify-zlibworks perfectly for me, it uses pako and has the exact same api as zlib. After I struggled for days with compressing/decompressing zlib encoded payloads in client side with pako, I can say that browserify-zlib is really convenient.

Browserify-zlib非常适合我,它使用 pako 并具有与 zlib 完全相同的 API。在我用 pako 在客户端压缩/解压缩 zlib 编码的有效负载方面挣扎了几天之后,我可以说 browserify-zlib 真的很方便。

回答by summer

you should see zlib rfc from:zlib rfc

你应该看到 zlib rfc 来自:zlib rfc

the javascript inflate code I tested:inflate in Javascriptthe java code I wrote:

我测试的 javascript 膨胀代码:在 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;
}

The very Important thing is in deflate in Java you must cut the head 2 byte,foot 4 byte,to get the raw deflate.

非常重要的是在 Java 中的 deflate 中,您必须将头部 2 字节,脚部 4 字节切掉,以获得原始的 deflate。