Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中解压缩gzip和zlib字符串_Javascript_Gzip_Zlib_Compression_Tmx - Fatal编程技术网

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

在javascript中解压缩gzip和zlib字符串,javascript,gzip,zlib,compression,tmx,Javascript,Gzip,Zlib,Compression,Tmx,我想从tmx文件中获取压缩层数据。谁知道javascript中解压缩gzip和zlib字符串的库?我试过了,但不管用。例如,tmx文件中的图层数据为: <data encoding="base64" compression="zlib"> eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ== </data> 它运行时显示错误消息“unsupported compression method”。但我尝试使用在线工具进行解压缩,因为

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

  <data encoding="base64" compression="zlib">
       eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==
  </data>
它运行时显示错误消息“unsupported compression method”。但我尝试使用在线工具进行解压缩,因为它返回正确的字符串。

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

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();
是一个完整且现代化的
Zlib
端口

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

获取并可以通过tearray进行解压缩,如下所示:

<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>
运行示例:


更高级的是,对于任何使用Ruby on Rails的人,如果他们想将压缩编码的数据发送到浏览器,然后通过浏览器上的Javascript将其解压缩,这里有一个。

,我将以上两个优秀的答案结合到下面的解决方案中。以下是my application controller中的Rails服务器代码,它在通过@变量将字符串发送到浏览器的.html.erb文件之前对字符串进行压缩和编码:

下面是使用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));
}
下面是对解压函数的javascript调用,该函数希望解压存储在隐藏HTML字段中的数据:

my_answer = uncompress('my_hidden_field');
以下是Rails application.js文件中调用pako.min.js的条目,它位于/vendor/assets/javascripts目录中:

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


总之,在我这一方,一切都是可行的!:-)

感谢您提出的问题(即使将压缩数据编码为base64似乎有点反常,因为base64是一种负压缩格式;我可以看到应用程序…)html示例可能无法正常工作,因为没有定义
pako
。API可能已经更改,它现在使用
require
了。注意:
String.fromCharCode.apply(null,新的Uint16Array(数据))
对于长输入失败(超过最大调用堆栈大小,JavaScript)。这应该适用于更长的数组:对于长字符串,使用
var String=new textdecker(“utf-8”)。解码(数据)
如本文所述:我遇到了一个问题。当我使用pako解压时,英文是可以的,但当遇到中文时,中文字符串将变成无法读取的代码。我不知道为什么。gzip压缩将删除图表集信息??pako不在ie 11上工作:(
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));
}
my_answer = uncompress('my_hidden_field');
//= require pako.min