
zip类
public class ZipClass

{

/**//// <summary>
/// 压缩方法
/// </summary>
/// <param name="strPath">要压缩文件夹</param>
/// <param name="strFileName">生成的文件名</param>
/// <param name="PassWord">密码</param>
/// <returns>1 成功 -1输入的压缩文件夹为空 -2 输入的压缩文件夹目录不存在!</returns>
public static int Zip(string strPath, string strFileName, string PassWord)

{
if (!String.IsNullOrEmpty(strPath))

{
//throw new Exception("输入的压缩文件夹为空");
return -1;
}
if (!Directory.Exists(strPath))

{
//throw new Exception("输入的压缩文件夹目录不存在!");
return -2;
}
if (!File.Exists(strFileName))

{
File.Create(strFileName);
}
ZipOutputStream zip = new ZipOutputStream(File.Create(strFileName));
if (!string.IsNullOrEmpty(PassWord))
zip.Password = PassWord;
IList<FileInfo> list = new List<FileInfo>();
GetFileList(strPath, list);
try

{
byte[] buffer = new byte[2048];
int count = 0;
foreach (FileInfo fi in list)

{
count = 0;
using (FileStream fs = File.OpenRead(fi.FullName))

{
ZipEntry entry = new ZipEntry(fi.FullName.Substring(strPath.Length + 1));
entry.DateTime = fi.LastWriteTime;
entry.Size = fs.Length;
zip.PutNextEntry(entry);
while ((count = fs.Read(buffer, 0, 2048)) > 0)

{
zip.Write(buffer, 0, count);
}
}
}
}
catch

{
throw;
}
finally

{
zip.Finish();
zip.Close();
}
return 1;
}
private static void GetFileList(string strPath, IList<FileInfo> list)

{
DirectoryInfo di = new DirectoryInfo(strPath);
foreach (DirectoryInfo di1 in di.GetDirectories())

{
GetFileList(di1.FullName, list);
}
foreach (FileInfo fi in di.GetFiles())

{
list.Add(fi);
}
}

/**//// <summary>
/// 解压缩文件
/// </summary>
/// <param name="strFileName">压缩文件</param>
/// <param name="strPath">目标目录</param>
/// <param name="PassWord">密码</param>
/// <returns>1 成功 -1输入的压缩文件夹为空 -2 输入的解压缩文件夹目录不存在!-3 文件不存在</returns>
public static int UnZip(string strFileName, string strPath, string PassWord)

{
if (!String.IsNullOrEmpty(strPath))

{
//throw new Exception("输入的压缩文件夹为空");
return -1;
}
if (!Directory.Exists(strPath))

{
//throw new Exception("输入的压缩文件夹目录不存在!");
return -2;
}
if (!File.Exists(strFileName))

{
//throw new Exception("文件:" + strFileName + "不存在!");
return -3;
}
ZipEntry entry;
ZipInputStream zis = null;
try

{
zis = new ZipInputStream(File.Open(strFileName, FileMode.Open));
if (!string.IsNullOrEmpty(PassWord))
zis.Password = PassWord;
byte[] buffer = new byte[2048];
int count = 0;
while ((entry = zis.GetNextEntry()) != null)

{
CreateDirList(entry.Name, strPath);
string strPath1 = strPath + "\\" + entry.Name;
using (FileStream streamWriter = File.Create(strPath1))

{
while ((count = zis.Read(buffer, 0, 2048)) > 0)

{
streamWriter.Write(buffer, 0, count);
}
}
File.SetLastWriteTime(strPath1, entry.DateTime);
}
}
catch

{
throw;
}
finally

{
if (zis != null)
zis.Close();
}
return 1;
}
private static void CreateDirList(string filename, string basePath)

{
string dirName = basePath;
string[] dirlevelname = filename.Split('\\');
for (int i = 0; i < dirlevelname.Length - 1; i++)

{
dirName += "\\" + dirlevelname[i];
if (Directory.Exists(dirName))

{
continue;
}
Directory.CreateDirectory(dirName);
}
}
} 没有什么说的,直接上代码.
转载于:https://www.cnblogs.com/LifelongLearning/archive/2008/01/03/1025131.html