压缩 工具类
Posted yangpeng-jingjing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了压缩 工具类相关的知识,希望对你有一定的参考价值。
public class SharpZip
{
/// <summary>
/// 压缩
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public string Compress(string param)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
MemoryStream ms = new MemoryStream();
Stream stream = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(ms);
try
{
stream.Write(data, 0, data.Length);
}
finally
{
stream.Close();
ms.Close();
}
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// 解压
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public string Decompress(string param)
{
string commonString = "";
byte[] buffer = Convert.FromBase64String(param);
MemoryStream ms = new MemoryStream(buffer);
Stream sm = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(ms);
//这里要指明要读入的格式,要不就有乱码
StreamReader reader = new StreamReader(sm, System.Text.Encoding.UTF8);
try
{
commonString = reader.ReadToEnd();
}
finally
{
sm.Close();
ms.Close();
}
return commonString;
}
}
以上是关于压缩 工具类的主要内容,如果未能解决你的问题,请参考以下文章