Lua 从字符串中解压 gzip

Lua decompress gzip from string

我在从 lua 中的字符串解压缩 gzip 时遇到一些问题。 (mb不好理解)

一个 Web 服务的响应是 base64 编码的 gzip 字符串,作为示例,我在 C# 上获得了一些代码。

    public static string Decompress(byte[] value, Encoding Encoding = null)
    {
        if (value == null) return null;
        Encoding = Encoding ?? System.Text.Encoding.Unicode;
        using (var inputStream = new MemoryStream(value)) 
        using (var outputStream = new MemoryStream())                              
        {
            using (var zip = new GZipStream(inputStream, CompressionMode.Decompress))  
            {
                byte[] bytes = new byte[4096];
                int n;
                while ((n = zip.Read(bytes, 0, bytes.Length)) != 0)
                {
                    outputStream.Write(bytes, 0, n);
                }
                zip.Close();
            }
            return Encoding.GetString(outputStream.ToArray());    
        }
    }

    static void Main(string[] args)
    {
        const string encodedText = "H4sIAAAAAAAEAHMNCvIPUlRwzS0oqVQoLinKzEtXyC9SyCvNyYFxM/OAqKC0RKEgsSgxN7UktQgAwOaxgjUAAAA=";

        byte[] decodedBytes = Convert.FromBase64String(encodedText);

        var decodedString = Decompress(decodedBytes, Encoding.UTF8);

        Console.WriteLine(decodedString);

    }

我尝试使用 lua(在 nginx 上)并从字节

的 base64 字符串数组中创建
local byte_table={};
base64.base64_decode(res_string):gsub(".", function(c){
 table.insert(byte_table, string.byte(c))
})

但是 zlib 有一些问题。

请帮助我了解如何在 lua 中使用 IO 流并解压缩 gzip。

I try to do this with lua (on nginx) and make from base64 string array of byte

不,您正在用一堆数字制作 Lua table。

解码 base64 并将整个结果字符串提供给 zlib。

我使用 http://luaforge.net/projects/lzlib/ 进行 gzip 解压缩: local result = zlib.decompress(str,31)