public bool ZipFile(string rarName) { bool flag = false; string dirPath = AppDomain.CurrentDomain.BaseDirectory; string zipPath = dirPath + @"temp\"; ZipClass ZC = new ZipClass(); ZC.IgnoreFile = zipPath + rarName + ".rar"; int dirsCount = 0, filesCount = 0; FileSystemObject.GetTotalDF(dirPath, ref dirsCount, ref filesCount); ZipClass.zipTotal = dirsCount;//只用目录数就可以了 ZipClass.zipProgress = 0; DirectoryInfo dr = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); if (!Directory.Exists(zipPath)) { Directory.CreateDirectory(zipPath); } if (ZC.Zip(dirPath, zipPath + rarName + ".rar")) { flag = true; } return(flag); }
public static ZipResult Zip(ZipConfig cfg) { if (string.IsNullOrEmpty(cfg.ZipSrc)) { return(cfg.Result.Failed("来源路径不能为空")); } if (string.IsNullOrEmpty(cfg.ZipSave)) { return(cfg.Result.Failed("Zip文件保存路径不能为空")); } if (File.Exists(cfg.ZipSrcPath)) { cfg.ZipType = "file"; } else if (Directory.Exists(cfg.ZipSrcPath)) { cfg.ZipType = "dir"; } else { return(cfg.Result.Failed("需要压缩的文件或目录[" + cfg.ZipSrc + "]不存在")); } if (!Directory.Exists(Path.GetDirectoryName(cfg.ZipSavePath))) { Directory.CreateDirectory(Path.GetDirectoryName(cfg.ZipSavePath)); } //ZipTaskList.Add(cfg); //----------开始压缩 switch (cfg.ZipType) { case "file": if (cfg.IsAsync) { new System.Threading.Thread(AsyncZipFile).Start(cfg); } else { ZipFile(cfg); } break; case "dir": default: int dirsCount = 0, filesCount = 0; FileSystemObject.GetTotalDF(cfg.ZipSrcPath, ref dirsCount, ref filesCount); cfg.Result.P_Total = dirsCount; //只计目录数 if (cfg.IsAsync) { new System.Threading.Thread(AsyncZipFileDictory).Start(cfg); } else { ZipFileDictory(cfg); } break; } return(cfg.Result); }