解压缩 GZIP 流

Decompressing GZIP stream

我正在尝试解压缩 GZipped 字符串,它是网络服务响应的一部分。我的字符串是:

"[31,-117,8,0,0,0,0,0,0,0,109,-114,65,11,-62,48,12,-123,-1,75,-50,-61,-42,-127,30,122,21,111,-126,94,60,-119,-108,-72,102,44,-48,-75,-93,-21,100,56,-6,-33,-19,20,20,101,57,37,95,-14,94,-34,4,-63,-5,-72,-73,-44,-110,-117,-96,38,-88,26,-74,38,-112,3,117,-7,25,-82,5,24,-116,56,-97,-44,108,-23,28,24,-44,-85,83,34,-41,97,-88,24,-99,23,36,124,-120,94,99,-120,15,-42,-91,-108,91,45,-11,70,119,60,-110,21,-20,12,-115,-94,111,-80,-93,89,-41,-65,-127,-82,76,41,51,-19,52,90,-5,69,-85,76,-96,-128,64,22,35,-33,-23,-124,-79,-55,-1,-2,-10,-87,0,55,-76,55,10,-57,122,-9,73,42,-45,98,-44,5,-77,101,-3,58,-91,39,38,51,-15,121,21,1,0,0]"

我正在尝试使用以下方法解压缩该字符串:

public static string UnZip(string value)
        {
            // Removing brackets from string
            value = value.TrimStart('[');
            value = value.TrimEnd(']');

            //Transform string into byte[]
            string[] strArray = value.Split(',');
            byte[] byteArray = new byte[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i][0] != '-')
                    byteArray[i] = Convert.ToByte(strArray[i]);
                else
                {
                    int val = Convert.ToInt16(strArray[i]);
                    byteArray[i] = (byte)(val + 256);
                }
            }

            //Prepare for decompress
            System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Decompress);

            //Reset variable to collect uncompressed result
            byteArray = new byte[byteArray.Length];

            //Decompress
            int rByte = sr.Read(byteArray, 0, byteArray.Length);

            //Transform byte[] unzip data to string
            System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
            //Read the number of bytes GZipStream red and do not a for each bytes in
            //resultByteArray;
            for (int i = 0; i < rByte; i++)
            {
                sB.Append((char)byteArray[i]);
            }
            sr.Close();
            ms.Close();
            sr.Dispose();
            ms.Dispose();
            return sB.ToString();
        }

该方法是以下方法的修改版本link: http://www.codeproject.com/Articles/27203/GZipStream-Compress-Decompress-a-string

遗憾的是,该方法的结果是损坏的字符串。更具体地说,我知道输入字符串包含一个压缩的 JSON 对象,而输出字符串只有一些预期的字符串:

"{\"rootElement\":{\"children\":[{\"children\":[],\"data\":{\"fileUri\":\"file:////Luciano/e/orto_artzi_2006_0_5_pixel/index/shapefiles/index_cd20/shp_all/index_cd2.shp\",\"relativePath\":\"/i"

知道问题出在哪里以及如何解决吗?

尝试

public static string UnZip(string value)
{
    // Removing brackets from string
    value = value.TrimStart('[');
    value = value.TrimEnd(']');

    //Transform string into byte[]
    string[] strArray = value.Split(',');
    byte[] byteArray = new byte[strArray.Length];
    for (int i = 0; i < strArray.Length; i++)
    {
        byteArray[i] = unchecked((byte)Convert.ToSByte(strArray[i]));
    }

    //Prepare for decompress
    using (System.IO.MemoryStream output = new System.IO.MemoryStream())
    {
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray))
        using (System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
        {
            sr.CopyTo(output);
        }

        string str = Encoding.UTF8.GetString(output.GetBuffer(), 0, (int)output.Length);
        return str;
    }
}

MemoryBuffer() 不是 "duplicate" byteArray,而是直接由它支持,因此您不能重复使用 byteArray

我要补充一点,我觉得很有趣,他们 "compressed" 将 277 个字符的 json 转换为 620 个字符的字符串化字节数组。

顺便说一句,这种方法的内存占用是天方夜谭...要解压的620个字符串(实际上是一个277字节的数组)导致创建strings/arrays 总大小为4887字节(包括620个初始字符串)(免责声明:GC可以在方法执行期间回收部分内存)。这对于 277 字节的字节数组是可以的...但是对于更大的字节数组,内存占用将变得相当大。

根据 Xanatos 在 C# 中的回答,稍微修改为 return 一个简单的字节数组。这需要一个 gzip 压缩字节数组和 returns 膨胀的压缩数组。

public static byte[] Decompress(byte[] compressed_data)
{
    var outputStream = new MemoryStream();
    using (var compressedStream = new MemoryStream(compressed_data))
    using (System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(
        compressedStream, System.IO.Compression.CompressionMode.Decompress))
    {
        sr.CopyTo(outputStream);
        outputStream.Position = 0;
        return outputStream.ToArray();
    }
}