Javascript 在javascript中解压gzip和zlib字符串

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

Decompress gzip and zlib string in javascript

javascriptgzipzlibcompressiontmx

提问by Toan Nguyen

I want to get compress layer data from tmx file . Who knows libraries for decompress gzip and zlib string in javascript ? I try zlibbut it doesn't work for me . Ex , layer data in tmx file is :

我想从 tmx 文件中获取压缩层数据。谁知道在 javascript 中解压 gzip 和 zlib 字符串的库?我尝试zlib但它对我不起作用。例如,tmx 文件中的图层数据为:

  <data encoding="base64" compression="zlib">
       eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==
  </data>

My javascript code is

我的 javascript 代码是

var base64Data = "eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==";
var compressData = atob(base64Data);
var inflate = new Zlib.Inflate(compressData);
var output = inflate.decompress();

It runs with displays message error "unsupported compression method" . But I try decompress with online tool as http://i-tools.org/gzip, it returns correct string.

它运行时显示消息错误“不支持的压缩方法”。但我尝试使用在线工具解压缩http://i-tools.org/gzip,它返回正确的字符串。

采纳答案by Toan Nguyen

I can solve my problem by zlib. I fix my code as below

我可以通过zlib解决我的问题。我修复我的代码如下

var base64Data = "eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==";
var compressData = atob(base64Data);
var compressData = compressData.split('').map(function(e) {
    return e.charCodeAt(0);
});
var inflate = new Zlib.Inflate(compressData);
var output = inflate.decompress();

回答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 Andy Duncan

For anyone using Ruby on Rails, who wants to send compressed encoded data to the browser, then uncompress it via Javascript on the browser, I've combined both excellent answers above into the following solution. Here's the Rails server code in my application controller which compresses and encodes a string before sending it the browser via a @variableto a .html.erbfile:

对于任何使用 Ruby on Rails 的人,如果他们想要将压缩的编码数据发送到浏览器,然后在浏览器上通过 Javascript 解压缩它,我已将上述两个出色的答案合并到以下解决方案中。这是我的应用程序控制器中的 Rails 服务器代码,它在通过@variable将字符串发送到浏览器之前压缩和编码字符串到.html.erb文件:

require 'zlib'
require 'base64'

    def compressor (some_string)
        Base64.encode64(Zlib::Deflate.deflate(some_string))
    end

Here's the Javascript function, which uses pako.min.js:

这是使用 pako.min.js 的 Javascript 函数:

function uncompress(input_field){
    base64data = document.getElementById(input_field).innerText;
    compressData = atob(base64data);
    compressData = compressData.split('').map(function(e) {
        return e.charCodeAt(0);
    });
    binData = new Uint8Array(compressData);
    data = pako.inflate(binData);
    return String.fromCharCode.apply(null, new Uint16Array(data));
}

Here's a javascript call to that uncompress function, which wants to unencode and uncompress data stored inside a hidden HTML field:

这是对该解压缩函数的 javascript 调用,它想要解压缩和解压缩存储在隐藏 HTML 字段中的数据:

my_answer = uncompress('my_hidden_field');

Here's the entry in the Rails application.jsfile to call pako.min.js, which is in the /vendor/assets/javascriptsdirectory:

下面是Rails的入门的application.js文件来调用pako.min.js,这是在/供应商/资产/ JavaScript的目录:

//= require pako.min

And I got the pako.min.jsfile from here:

我从这里得到了pako.min.js文件:

https://github.com/nodeca/pako/tree/master/dist

https://github.com/nodeca/pako/tree/master/dist

All works at my end, anyway! :-)

无论如何,一切都在我的最后!:-)