简体   繁体   中英

DotNetZip Library and C# BackgroundWorker to report progress

Using DotNetZip library I'm having difficulty trying to get this implemented with the BackgroundWorker

On the DotNetZip Documentation it shows how to Unzip an archive but ot how to Zip and report progress.

My attempt

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

You need to handle the ZipFile.AddProgress and ZipFile.SaveProgress events. Something like this: (check the documentation links for details and code samples)

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM