Game Development Reference
In-Depth Information
using (GZipStream zipStream = new GZipStream(output,
CompressionMode.Compress, true))
{
zipStream.Write(input, 0, input.Length);
}
return output.ToArray();
}
}
catch (Exception)
{
return null;
}
}
Decompression is handled in the same way as compression, except the
CompressionMode.Decompress enum value is used. The first step is to read the initial
four data bytes from the stream as an integer describing the buffer size for the
decompressed data. Then the data buffer is created and the input data is decom-
pressed and read into it.
internal static byte[] DecompressData(byte[] input)
{
try
{
using (MemoryStream inputData = new MemoryStream(input))
{
byte[] lengthData = new byte[4];
if (inputData.Read(lengthData, 0, 4) == 4)
{
int decompressedLength = BitConverter.ToInt32(lengthData, 0);
using (GZipStream zipStream = new GZipStream(inputData,
CompressionMode.Decompress))
{
byte[] decompressedData = new byte[decompressedLength];
if (zipStream.Read(decompressedData,
0,
decompressedLength) == decompressedLength)
{
return decompressedData;
}
Search Nedrilad ::




Custom Search