DotNetZip 库和 C# BackgroundWorker 报告进度

问题描述 投票:0回答:1

使用 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;
    }
c# backgroundworker dotnetzip
1个回答
3
投票

您需要处理 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");
}
© www.soinside.com 2019 - 2024. All rights reserved.