문제

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");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top