我正在尝试使用 .NET 中的 INFLATE 压缩流DeflateStream
。InvalidDataException
尽管我知道我传递的数据已经被 DEFLATE 算法正确处理(它已经过测试),但我的代码抛出了一个错误。我使用DeflateStream
不正确吗?我的代码如下:
public byte[] Inflate(byte[] deflateArr)
{
MemoryStream ms;
// try to create a MemoryStream from a byte array
try
{
ms = new MemoryStream(deflateArr);
}
catch (ArgumentNullException)
{
return null;
}
// create a deflatestream and pass it the memory stream
DeflateStream ds;
try
{
ds = new DeflateStream(ms, CompressionMode.Decompress);
}
catch (ArgumentNullException)
{
return null;
}
catch (ArgumentException)
{
return null;
}
// create a bytes array and read into it
byte[] bytes = new byte[4096];
try
{
ds.Read(bytes, 0, 4096);
}
catch (ArgumentNullException)
{
return null;
}
catch (InvalidOperationException)
{
return null;
}
catch (ArgumentOutOfRangeException)
{
return null;
}
catch (InvalidDataException)
{
return null;
}
// close the memory stream
ms.Close();
// close the deflate stream
ds.Close();
return bytes;
}