public void DeCompress(string zipFileName, string targetDirectory, string password, Action <string, string, string, float> OnProgress, Action <string, string> OnComplete, Action <string, string, Exception> OnError) { //新建任务; task = new UZipTask(); task.status = UZipTask.TaskStatus.None; task.srcPath = zipFileName; task.targetPath = targetDirectory; task.OnProgress = OnProgress; task.OnComplete = OnComplete; task.OnError = OnError; //开启子线程 thread = new Thread(() => { try { ZipFile fileInfo = new ZipFile(zipFileName); totalCount = (int)fileInfo.Count; Debug.Log("totalCount=" + totalCount); FastZipEvents events = new FastZipEvents(); events.ProcessFile = this.OnFileProgress; events.Progress = this.OnProgress; events.ProcessDirectory = this.OnDirectoryProgress; events.FileFailure = this.OnFileFailed; events.DirectoryFailure = this.OnDirectoryFailed; events.CompletedFile = this.OnFileCompleted; FastZip zipHelper = new FastZip(events); zipHelper.Password = password; zipHelper.ExtractZip(zipFileName, targetDirectory, ""); task.status = UZipTask.TaskStatus.Done; } catch (Exception ex) { Debug.LogError("DeCompress Thread Error: " + ex); task.status = UZipTask.TaskStatus.Error; task.exception = ex; } }); thread.Start(); }
int totalCount = 0; //需要压缩的文件数量; public void Compress(string sourceDirectory, string zipFileName, string password, Action <string, string, string, float> OnProgress, Action <string, string> OnComplete, Action <string, string, Exception> OnError) { //新建任务; task = new UZipTask(); task.status = UZipTask.TaskStatus.None; task.srcPath = sourceDirectory; task.targetPath = zipFileName; task.OnProgress = OnProgress; task.OnComplete = OnComplete; task.OnError = OnError; //开启子线程 thread = new Thread(() => { try { totalCount = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories).Length; Debug.Log("totalCount=" + totalCount); FastZipEvents events = new FastZipEvents(); events.ProcessFile = this.OnFileProgress; events.ProcessDirectory = this.OnDirectoryProgress; events.FileFailure = this.OnFileFailed; events.DirectoryFailure = this.OnDirectoryFailed; events.CompletedFile = this.OnFileCompleted; FastZip zipHelper = new FastZip(events); zipHelper.Password = password; zipHelper.CreateEmptyDirectories = true; zipHelper.CreateZip(zipFileName, sourceDirectory, true, ""); task.status = UZipTask.TaskStatus.Done; } catch (Exception ex) { Debug.LogError("Compress Thread Error: " + ex); task.status = UZipTask.TaskStatus.Error; task.exception = ex; } }); thread.Start(); }