Pergunta

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;
    }
Foi útil?

Solução

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");
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top