Compression/Decompression string with C#

后端 未结 6 1918
情歌与酒
情歌与酒 2020-11-22 17:14

I am newbie in .net. I am doing compression and decompression string in C#. There is a XML and I am converting in string and after that I am doing compression and decompress

6条回答
  •  执笔经年
    2020-11-22 17:46

    For those who still getting The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. ERROR and if your string was zipped using php you'll need to do something like:

           public static string decodeDecompress(string originalReceivedSrc) {
            byte[] bytes = Convert.FromBase64String(originalReceivedSrc);
    
            using (var mem = new MemoryStream()) {
                //the trick is here
                mem.Write(new byte[] { 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0, 8);
                mem.Write(bytes, 0, bytes.Length);
    
                mem.Position = 0;
    
                using (var gzip = new GZipStream(mem, CompressionMode.Decompress))
                using (var reader = new StreamReader(gzip)) {
                    return reader.ReadToEnd();
                    }
                }
            }
    

提交回复
热议问题