public string DeflateDecompress(string strSource)
{
byte[] buffer = Convert.FromBase64String(strSource);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms,System.IO.Compression.CompressionMode.Decompress))
{
stream.Flush();
int nSize = 16 * 1024 + 256; //假设字符串不会超过16K
byte[] decompressBuffer = new byte[nSize];
int nSizeIncept = stream.Read(decompressBuffer, 0, nSize);
stream.Close();
return System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept); //转换为普通的字符串
}
}
}
此函数在在有些情况下是不能正确执行的,居然有个坑爹的解决方案,就是把 ms.Position = 0;
改为 ms.Position = 2;跳过两个字节,至于原理,俺不懂,但是调试通过了
转载于:https://blog.51cto.com/6708745/1157448