1

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;
    }
MPelletier
  • 16,256
  • 15
  • 86
  • 137
IEnumerable
  • 3,610
  • 14
  • 49
  • 78
  • You can only report progress you can measure. You can only measure progress yourself as the number of files and/or folders you have added. Beyond that, you'd need DotNetZip to tell you how far along it is in order for you to tell the user. Does DotNetZip provide asynchronous versions of the methods that you're calling that raise ProgressChanged events or the like? If not then you're out of luck because you can't report what you can't measure. Perhaps a ProgressBar with the Style set to Marquee is the best you can do. – jmcilhinney Feb 13 '14 at 03:38

1 Answers1

2

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");
}
Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
  • I only just got back home to check my Messages. The link to SaveProgress event helped me understand it more. Thanks – IEnumerable Feb 13 '14 at 04:42