使用DotNetZip库我很难尝试使用BackgroundWorker实现此功能
在DotNetZip Documentation上,它会显示如何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;
}
答案 0 :(得分:2)
您需要处理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");
}