注意:用了开源的CL.IO.Zip库
pbYSJD是进度条的控件名
btnImport是按钮控件名,当压缩结束之后,使按钮处于激活状态,否则无法点击按钮。
/// <summary> /// 压缩文件夹并复制到制定目录 /// </summary> /// <param name="strPath">待压缩的文件夹路径</param> /// <param name="strZipPath">需要复制到的目录路径(该路径需要带压缩文件名)</param> private void ImportZip(string strPath,string strZipPath) { ZipHandler handler = ZipHandler.GetInstance(); TaskFactory fastory = new TaskFactory(); Task[] tasks = new Task[] { fastory.StartNew(() => { handler.PackDirectory(strPath, strZipPath, (num) => { pbYSJD.Value = Convert.ToInt32(num); }); }) }; fastory.ContinueWhenAll(tasks, TasksEnded); } private void TasksEnded(Task[] tasks) { MessageBox.Show("压缩完成", "提示"); btnImport.Enabled = true; }