程序集需要添加的引用是:ICSharpCode.SharpZipLib.dll
使用实例:
//执行压缩 ClsZip.Zip(downZipPath, sourceTopPath, 1, "", allFilePath); //执行下载 ctrlDcAllGrxx.download(downZipPath, true);
压缩文件的类ClsZip:
public class ClsZip
{ /// <summary> /// 生成压缩文件 /// </summary> /// <param name="strZipPath">生成的zip文件的路径,包括文件名</param> /// <param name="strSourceTopDirectoryPath">要压缩的源文件的上级目录</param> /// <param name="intZipLevel">T压缩等级</param> /// <param name="strPassword">压缩包解压密码,不设密码""</param> /// <param name="filesOrDirectoriesPaths">源文件路径</param> /// <returns></returns> public static bool Zip(string strZipPath, string strSourceTopDirectoryPath, int intZipLevel, string strPassword,
List<string> filesOrDirectoriesPaths) { try { List<string> AllFilesPath = new List<string>(); if (filesOrDirectoriesPaths.Count > 0) // get all files path { for (int i = 0; i < filesOrDirectoriesPaths.Count; i++) { if (File.Exists(filesOrDirectoriesPaths[i])) { AllFilesPath.Add(filesOrDirectoriesPaths[i]); } else if (Directory.Exists(filesOrDirectoriesPaths[i])) { GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath); } } } if (AllFilesPath.Count > 0) { ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath)); zipOutputStream.SetLevel(intZipLevel); zipOutputStream.Password = strPassword; for (int i = 0; i < AllFilesPath.Count; i++) { string strFile = AllFilesPath[i].ToString(); try { if (strFile.Substring(strFile.Length - 1) == "") //folder { string strFileName = strFile.Replace(strSourceTopDirectoryPath, ""); if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(1); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); } else //file { FileStream fs = File.OpenRead(strFile); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string strFileName = strFile.Replace(strSourceTopDirectoryPath, ""); if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(0); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); zipOutputStream.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); } } catch { continue; } } zipOutputStream.Finish(); zipOutputStream.Close(); return true; } else { return false; } } catch { return false; } } /// <summary> /// Gets the directory files. /// </summary> /// <param name="strParentDirectoryPath">源文件路径</param> /// <param name="AllFilesPath">所有文件路径</param> public static void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath) { string[] files = Directory.GetFiles(strParentDirectoryPath); for (int i = 0; i < files.Length; i++) { AllFilesPath.Add(files[i]); } string[] directorys = Directory.GetDirectories(strParentDirectoryPath); for (int i = 0; i < directorys.Length; i++) { GetDirectoryFiles(directorys[i], AllFilesPath); } if (files.Length == 0 && directorys.Length == 0) //empty folder { AllFilesPath.Add(strParentDirectoryPath); } }
}
参考:
ZipOutputStream类(1)
如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类。ZipOutputStream是OutputStream的子类,常用操作方法如表所示。
ZipOutputStream类的常用方法
序号
|
方 法
|
类型
|
描 述
|
1
|
public ZipOutputStream
(OutputStream out)
|
构造
|
创建新的ZIP输出流
|
2
|
public void putNextEntry
(ZipEntry e) throws IOException
|
普通
|
设置每一个
ZipEntry对象
|
3
|
public void setComment
(String comment)
|
普通
|
设置ZIP文件的注释
|
|
请发表评论