I am trying to utilize the INFLATE compression stream in .NET using a DeflateStream
. My code throws an InvalidDataException
although I know that the data I am passing has been correctly processed by the DEFLATE algorithm (it has been tested). Am I using the DeflateStream
incorrectly? My code is as follows:
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;
}