using System.IO;
using System.IO.Compression;
using System.Text; namespace CS.Utility
{
/// <summary>
/// 压缩与解压缩处理
/// </summary>
public static class Compressor
{ /// <summary>
/// 压缩字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] Compress(string str)
{
var data = Encoding.UTF8.GetBytes(str);
return Compress(data);
} /// <summary>
/// 压缩二进制流
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Compress(byte[] data)
{
using (var memoryStream = new MemoryStream())
{
using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
compressionStream.Write(data, , data.Length);
compressionStream.Flush();
}
//必须先关了compressionStream后才能取得正确的压缩流
return memoryStream.ToArray();
}
} /// <summary>
/// 解压二进制流
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Depress(byte[] data)
{
using (var memoryStream = new MemoryStream(data))
using (var outStream = new MemoryStream())
{
using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
compressionStream.CopyTo(outStream);
compressionStream.Flush();
}
return outStream.ToArray();
}
}
}
}
参考资料:http://msdn.microsoft.com/zh-cn/library/ms404280(v=vs.110).aspx