首页 文章

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

提问于
浏览
18

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

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

我的javascript代码是

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

它运行时显示消息错误"unsupported compression method" . 但我尝试使用在线工具解压缩为http://i-tools.org/gzip,它返回正确的字符串 .

3 回答

  • 9

    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 .

  • 3

    我可以通过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();
    
  • 23

    对于任何使用Ruby on Rails的人,谁想要将压缩编码数据发送到浏览器,然后通过浏览器上的Javascript解压缩它,我的应用程序控制器中的Rails服务器代码压缩并编码字符串,然后通过浏览器发送它@variable到.html.erb文件:

    require 'zlib'
    require 'base64'
    
        def compressor (some_string)
            Base64.encode64(Zlib::Deflate.deflate(some_string))
        end
    

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

    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文件:

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

    无论如何,一切都在我的尽头! :-)

相关问题