标签:uri nis tool value gets 技术 convert === .com
最近在网上查了一下在.net中进行压缩和解压缩的方法,方法有很多,我找到了以下几种:
参考代码如下:
//========================================================================
// 类名: CommonCompress
/// <summary>
/// 用于对文件和字符串进行压缩
/// </summary>
/// <remarks>
/// 用于对文件和字符串进行压缩
/// </remarks>
/*=======================================================================
变更记录
序号 更新日期 开发者 变更内容
0001 2008/07/22 张 新建
=======================================================================*/
public class CommonCompress
{
/// <summary>
/// 压缩字符串
/// </summary>
/// <param name="strUncompressed">未压缩的字符串</param>
/// <returns>压缩的字符串</returns>
public static string StringCompress(string strUncompressed)
{
byte[] bytData = System.Text.Encoding.Unicode.GetBytes(strUncompressed);
MemoryStream ms = new MemoryStream();
Stream s = new GZipStream(ms, CompressionMode.Compress);
s.Write(bytData, 0, bytData.Length);
s.Close();
byte[] dataCompressed = (byte[])ms.ToArray();
return System.Convert.ToBase64String(dataCompressed, 0, dataCompressed.Length);
}
/// <summary>
/// 解压缩字符串
/// </summary>
/// <param name="strCompressed">压缩的字符串</param>
/// <returns>未压缩的字符串</returns>
public static string StringDeCompress(string strCompressed)
{
System.Text.StringBuilder strUncompressed = new System.Text.StringBuilder();
int totalLength = 0;
byte[] bInput = System.Convert.FromBase64String(strCompressed); ;
byte[] dataWrite = new byte[4096];
Stream s = new GZipStream(new MemoryStream(bInput), CompressionMode.Decompress);
while (true)
{
int size = s.Read(dataWrite, 0, dataWrite.Length);
if (size > 0)
{
totalLength += size;
strUncompressed.Append(System.Text.Encoding.Unicode.GetString(dataWrite, 0, size));
}
else
{
break;
}
}
s.Close();
return strUncompressed.ToString();
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="iFile">压缩前文件路径</param>
/// <param name="oFile">压缩后文件路径</param>
public static void CompressFile(string iFile, string oFile)
{
//判断文件是否存在
if (File.Exists(iFile) == false)
{
throw new FileNotFoundException("文件未找到!");
}
//创建文件流
byte[] buffer = null;
FileStream iStream = null;
FileStream oStream = null;
GZipStream cStream = null;
try
{
//把文件写进数组
iStream = new FileStream(iFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer = new byte[iStream.Length];
int num = iStream.Read(buffer, 0, buffer.Length);
if (num != buffer.Length)
{
throw new ApplicationException("压缩文件异常!");
}
//创建文件输出流并输出
oStream = new FileStream(oFile, FileMode.OpenOrCreate, FileAccess.Write);
cStream = new GZipStream(oStream, CompressionMode.Compress, true);
cStream.Write(buffer, 0, buffer.Length);
}
finally
{
//关闭流对象
if (iStream != null) iStream.Close();
if (cStream != null) cStream.Close();
if (oStream != null) oStream.Close();
}
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="iFile">压缩前文件路径</param>
/// <param name="oFile">压缩后文件路径</param>
public static void DecompressFile(string iFile, string oFile)
{
//判断文件是否存在
if (File.Exists(iFile) == false)
{
throw new FileNotFoundException("文件未找到!");
}
//创建文件流
FileStream iStream = null;
FileStream oStream = null;
GZipStream dStream = null;
byte[] qBuffer = new byte[4];
try
{
//把压缩文件写入数组
iStream = new FileStream(iFile, FileMode.Open);
dStream = new GZipStream(iStream, CompressionMode.Decompress, true);
int position = (int)iStream.Length - 4;
iStream.Position =