private void AddZipEnity(ZipEnity s, ZipArchive zip, int tota, ref int pros, Action <CompressEventArgs> progressCallback) { if (s.Type == EnityType.Dir) { var nodes = s.Nodes; if (nodes == null || nodes.Length == 0) { return; } foreach (var enity in nodes) { AddZipEnity(enity, zip, tota, ref pros, progressCallback); } return; } if (s.Type == EnityType.File) { using (FileStream fs = new FileStream(dirPath + s.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var enity = zip.CreateEntry(s.FullPath); using (var zips = enity.Open()) { fs.CopyTo(zips); zips.Flush(); arg.Progress = ((double)++pros) / tota; arg.Msg = s.FullPath; arg.Name = s.Name; progressCallback(arg); } } return; } }
public Task StartCompressAsync(string password = null, Action <CompressEventArgs> progressCallback = null) { return(Task.Factory.StartNew(() => { while (true) { if (File.Exists(savePath)) { File.Delete(savePath); System.Threading.Thread.Sleep(1); continue; } break; } if (progressCallback == null) { progressCallback = defaultCompressCall; } int tota; int pros = 0; var s = ZipEnity.GetZipFileEnity(dirPath, out tota); using (ZipArchive zip = ZipFile.Open(savePath, ZipArchiveMode.Create, encoding)) { AddZipEnity(s, zip, tota, ref pros, progressCallback); } arg.Progress = 1; arg.Msg = "压缩完成"; progressCallback(arg); })); }