DotNetZip Library和C#BackgroundWorker报告进度

时间:2014-02-13 03:14:11

标签: c# backgroundworker dotnetzip

使用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;
    }

1 个答案:

答案 0 :(得分:2)

您需要处理ZipFile.AddProgressZipFile.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");
}