C#中deflate解压不能正确进行!
以下函数为C#中的delate解压部分,输入为经base编码的deflate压缩数据,调试到base64解码后,写入文件流还是正确的,但解压流不正确,请高手指点。
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); //转换为普通的字符串
}
}
}
------解决方案--------------------System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept);
确定是使用的UTF8?可能是编码的问题。
------解决方案--------------------
static void Main(string[] args)
{
string str = "Hello World!";
Console.WriteLine("压缩前:" + str);
str = DeflateCompress(str);
Console.WriteLine("压缩前:" + str);
str = DeflateDecompress(str);
Console.WriteLine("解压后:" + str);
Console.ReadLine();
}
public static string DeflateCompress(string strSource)
{
&nb