解压缩GZIP流

时间:2016-03-01 10:51:37

标签: c# gzip gzipstream compression

我正在尝试解压缩GZipped字符串,这是来自Web服务的响应的一部分。我拥有的字符串是:

"[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();
        }

该方法是以下链接中的方法的修改版本: 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"

知道可能是什么问题以及如何解决它?

2 个答案:

答案 0 :(得分:4)

尝试

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()不会“复制”byteArray,但会直接受其支持,因此您无法重复使用byteArray

我要补充一点,我觉得很有趣,他们将一个277个字符的json“压缩”为一个620字符的字符串化字节数组。

作为旁注,这种方法的内存占用是不可能的...要解压缩的620字符串(实际上是一个277字节的数组)会导致创建字符串/数组总大小为4887字节(包括620初始字符串)(免责声明:GC可以在执行方法期间回收部分内存)。这对于277字节的字节数组是可以的......但对于较大的字节数组,内存占用将变得非常大。

答案 1 :(得分:0)

根据Xanatos在C#中的答案进行了稍微修改,以返回一个简单的字节数组。这将使用gzip压缩字节数组,并返回膨胀的gunzipped数组。

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();
    }
}