2010年12月26日 星期日

利用ICSharpCode.SharpZipLib 進行壓縮,解壓縮

來源處
--
using System;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;

/**////
/// Summary description for ICSharp
///

public class ICSharp
{
public ICSharp()
{
//
// TODO: Add constructor logic here
//
}
/**////
/// ??
///

/// ///
public string 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());
}
/**////
/// 解?
///

/// ///
public string 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;
}
}

沒有留言:

張貼留言