【问题标题】:Lua decompress gzip from stringLua从字符串中解压gzip
【发布时间】:2016-08-19 07:13:23
【问题描述】:

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

一个 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。

【问题讨论】:

    标签: string nginx lua gzip compression


    【解决方案1】:

    我尝试使用 lua(在 nginx 上)并从 base64 字节字符串数组制作

    不,你正在用一堆数字制作 Lua 表。

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

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

    【讨论】:

    • 在某处谷歌搜索。
    • local result = zlib.decompress(base64.base64_decode('H4sIAAAAAAAEAHMNCvIPUlRwzS0oqVQoLinKzEtXyC9SyCvNyYFxM/OAqKC0RKEgsSgxN7UktQgAwOaxgjUAAAA='),31); 错误!输入参数中的空字符串或空字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2017-10-10
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多