GZIP的压缩与解压缩代码:
public static class CompressionHelper
{
/// <summary>
/// Compress the byte[]
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static byte[] Compress(byte[] input)
{
byte[] output;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress))
{
gs.Write(input, 0, input.Length);
gs.Close();
output = ms.ToArray();
}
ms.Close();
}
return output;
}
/// <summary>
/// Decompress the byte[]
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static byte[] Decompress(byte[] input)
{
List<byte> output = new List<byte>();
using (MemoryStream ms = new MemoryStream(input))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
{
int readByte = gs.ReadByte();
while (readByte != -1)
{
output.Add((byte)readByte);
readByte = gs.ReadByte();
}
gs.Close();
}
ms.Close();
}
return output.ToArray();
}
}