Share via


zip folder in BackgroundWorker and show ProgressBar

Question

Saturday, October 5, 2013 8:30 AM

hello,

first of all im sorry for bad typing.

im going to zip folder via C# cod.

i make it, and now i want to add ProgressBar in my form.

this is sample my cod:

ZipFile.CreateFromDirectory("C:\d\SurceDir", "D:\d\DestinationDir");

so how can add this code to BackgroundWorker and show progressbar ?

im using BackgroundWorker, but is not complete:

{
            InitializeComponent();
            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;
            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }

 // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // Your background task goes here           
            ZipFile.CreateFromDirectory("C:\d\SurceDir", "D:\d\DestinationDir");
        }
        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

tnx.

001001110101110

All replies (4)

Sunday, October 6, 2013 1:03 PM âś…Answered

The method that you are using for extraction and compression provides no progress, it is either starting or done, which is why I provided links to the other methods.

If you determine the number of files that need to be compressed, and then compress them one at a time, you could report progress (update your progress bar) as each file is compressed.

If you want more granular progress bar updating, then you will need to determine the total number of bytes that need to be compressed, compress the files by whatever chunk size that you choose and then report progress at whatever level of detail that you wish.

It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


Saturday, October 5, 2013 1:31 PM

Since the CreateFromDirectory method does not directly support reporting progress, you will likely need to process the individual files themselves.  A sample can be found at: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchiveentry.aspx

It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


Saturday, October 5, 2013 1:38 PM

You may also want to take a look at the extension methods that are available, depending upon the level of progress that you want to utilize: http://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions.aspx

It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.


Sunday, October 6, 2013 6:20 AM

tnx Sir,

Sir, for extract and compress i have no problem.

just i want to add Progress Bar in my App ? :-s

Is there a way to do this?

001001110101110