/// <summary>
/// 压缩数据
/// </summary>
public class
gzip
{
/// <summary>
/// 将字节数组进行压缩后返回压缩的字节数组
/// </summary>
/// <param name="data">需要压缩的数组</param>
/// <returns>压缩后的数组</returns>
public static byte[] Compress(byte[] data)
{
MemoryStream stream = new MemoryStream();
GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress);
gZipStream.Write(data, 0, data.Length);
gZipStream.Close();
return stream.ToArray();
}
/// <summary>
/// 解压字符数组
/// </summary>
/// <param name="data">压缩的数组</param>
/// <returns>解压后的数组</returns>
public static byte[] Decompress(byte[] data)
{
MemoryStream stream = new MemoryStream();
GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
byte[] bytes = new byte[40960];
int n;
while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, n);
}
gZipStream.Close();
return stream.ToArray();
}
}