使用 DotNetZip 库,我很难尝试使用 BackgroundWorker 来实现此功能
在 DotNetZip 文档 上,它展示了如何
Unzip
存档,但不展示如何 Zip
和报告进度。
我的尝试
public void DoZIP()
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.ProgressChanged +=worker_ProgressChanged;
worker.DoWork += (o, e) =>
{
using (ZipFile zip = new ZipFile())
{
zip.StatusMessageTextWriter = System.Console.Out;
zip.AddFile("c:\somefile.txt", "/");
zip.AddDirectory("c:\somedir\", "/dir/");
zip.Save("c:\myzip.zip");
//worker.ReportProgress(5);
}
};
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.txtProgress.Text = this.txtProgress.Text + "\r\n" + "Completed :" + e.ProgressPercentage;
}
您需要处理 ZipFile.AddProgress 和 ZipFile.SaveProgress 事件。像这样的东西:(检查文档链接以获取详细信息和代码示例)
using (ZipFile zip = new ZipFile())
{
zip.AddProgress += (s, e) => {
// worker.ReportProgress(percentComplete);
};
zip.SaveProgress += (s, e) => {
// worker.ReportProgress(percentComplete);
};
zip.StatusMessageTextWriter = System.Console.Out;
zip.AddFile(@"c:\somefile.txt", "/");
zip.AddDirectory(@"c:\somedir\", "/dir/");
zip.Save(@"c:\myzip.zip");
}