【文件属性】:
文件名称:C#中压缩字符串
文件大小:204KB
文件格式:DLL
更新时间:2016-12-09 03:26:49
C# 压缩
使用以下代码进行压缩和解压
///
/// 利用ICSharpCode压缩
///
///
///
public static string IC_Compress(string param)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
//byte[] data = Convert.FromBase64String(param);
MemoryStream ms = new MemoryStream();
Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
try
{
stream.Write(data, 0, data.Length);
}
finally
{
stream.Close();
ms.Close();
}
return Convert.ToBase64String(ms.ToArray());
}
///
/// 利用ICSharpCode解压
///
///
///
public static string IC_decompress(string param)
{
string commonString="";
byte[] buffer=Convert.FromBase64String(param);
MemoryStream ms = new MemoryStream(buffer);
Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
//这里要指明要读入的格式,要不就有乱码
StreamReader reader = new StreamReader(sm,System.Text.Encoding.UTF8);
try
{
commonString=reader.ReadToEnd();
}
finally
{
sm.Close();
ms.Close();
}
return commonString;
}