您的位置:首页 > 编程语言 > C#

C#中用SharpZipLib.dll实现压缩解压2

2009-06-23 14:31 501 查看
/*******************************************************
版权所有:
用 途:文件压缩类

结构组成:

说 明:靠调用CSharpCode.SharpZipLib.dll类库来实现对文件的压缩和解压。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Threading;

namespace Framework.Utility.FileHelper
{
class SharpZipHelper
{
#region 压缩指定文件生成ZIP文件
/// <summary>
/// 压缩指定文件生成ZIP文件
/// </summary>
/// <param name="fileNamesToZip">待压缩文件列表(绝对路径)</param>
/// <param name="ZipedFileName">ZIP文件(绝对路径)</param>
/// <param name="CompressionLevel">压缩比</param>
/// <param name="password">密码</param>
/// <param name="comment">压缩文件注释文字</param>
/// <returns>返回错误信息</returns>
public static string ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password, string comment)
{
try
{
System.Text.RegularExpressions.Regex regPath = new System.Text.RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))");
if (!regPath.Match(ZipedFileName).Success)
{
File.Delete(ZipedFileName);
return "压缩文件的路径不正确!";
}
ZipOutputStream s = new ZipOutputStream(File.Open(ZipedFileName, FileMode.OpenOrCreate));
if (password != null && password.Length > 0)
s.Password = password;
if (comment != null && comment.Length > 0)
s.SetComment(comment);
s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression

foreach (string file in fileNamesToZip)
{
FileStream fs = File.OpenRead(file); //打开待压缩文件
if (!regPath.Match(ZipedFileName).Success)
{
File.Delete(ZipedFileName);
return "待压缩的文件路径不正确!";
}
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); //读取文件流
ZipEntry entry = new ZipEntry(Path.GetFileName(file)); //新建实例

entry.DateTime = DateTime.Now;

entry.Size = fs.Length;
fs.Close();

s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
catch(Exception err)
{
File.Delete(ZipedFileName);
return err.Message;
}
return "";
}
public static void ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel, string password)
{
ZipFile(fileNamesToZip, ZipedFileName, CompressionLevel, password, null);
}
public static void ZipFile(string[] fileNamesToZip, string ZipedFileName, int CompressionLevel)
{
ZipFile(fileNamesToZip, ZipedFileName, CompressionLevel, null);
}
#endregion

#region UnZipFile:解压缩ZIP文件到指定文件夹
/// <summary>
/// 解压缩ZIP文件到指定文件夹
/// </summary>
/// <param name="zipfileName">ZIP文件(物理路径)</param>
/// <param name="UnZipDir">解压文件夹(物理路径)</param>
/// <param name="password">压缩文件密码</param>
/// <returns>返回错误信息</returns>
public static string UnZipFile(string zipfileName, string UnZipDir, string password)
{
if (!File.Exists(zipfileName))
return "待解压的文件路径不存在!";
ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
if (password != null && password.Length > 0)
s.Password = password;
try
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
if (Directory.Exists(UnZipDir))
{
Directory.CreateDirectory(UnZipDir);
}
string directoryName = Path.GetDirectoryName(UnZipDir);
string pathname = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);

//生成解压目录
pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题
directoryName = directoryName + "\\" + pathname;
Directory.CreateDirectory(directoryName);

if (fileName != String.Empty)
{
//解压文件到指定的目录
FileStream streamWriter = File.Create(directoryName + "\\" + fileName);

int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
s.Close();
}
catch (Exception eu)
{
return eu.Message;
}
finally
{
s.Close();
}
return "";
}

public static void UnZipFile(string zipfileName, string UnZipDir)
{
UnZipFile(zipfileName, UnZipDir, null);
}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: