Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I got a form decompressing zip file using windows form C# & I would really like to add a ProgressBar to it, but unfortunately I have no idea how to write code that connects the ProgressBar to my decompress code to show the progress. I use a TextBox named txtsource to show the path of desired file, a button called btnbrowse to select the desired file & another button as btnextract to commence the decompression proccess.
here are my codes:

using System.IO;
using System.IO.Compression;

private void btnsource_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
txtsource.Text = openFileDialog1.FileName;
}
}

C#


private void btnextract_Click(object sender, EventArgs e)
{
txtsource.Text = openFileDialog1.FileName;

DialogResult result2 = folderBrowserDialog1.ShowDialog();
if (result2 == DialogResult.OK)
{
System.IO.Compression.ZipFile.ExtractToDirectory(openFileDialog1.FileName, folderBrowserDialog1.SelectedPath);
MessageBox.Show("Zip package has been extracted to\n" + txtsource.Text, " package extraction proccess Successfull!", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
}

What I have tried:

Well franckly, I really know little about ProgressBar & so far the articles I read to fix my problems weren't useful & I need to say that I'm a rookie programmer, so I would really appreciate your help. Thank you.
Posted
Updated 13-Apr-20 5:37am

Doing that with a genuine progress bar is going to be a little complex as this shows: c# - Extract an archive with progress bar? - Stack Overflow[^]
The first thing to do is move the actual work off the UI thread into a different thread - a BackgroundWorker[^] is simple to use - then use the code given in the SO link to provide progress indications via the worker ReportProgress method back to the UI thread event handler and that updates the progress bar.
 
Share this answer
 
The other problem with what you're doing is that ZipFile.ExtractToDirectory does all the work and itself does not report progress, so your progress bar is going to go from 0 to 100 with no step in between.

The only way you're going to get an progress displayed is if you're unzipping an entire list of files, one at a time. Then you can report progress on how far through the list you've gotten.

However, you can change the progress bar to be a Marquee type, so you get a constantly changing bar image to show that something is happening. You just can't show how far into the process the unzip is.
 
Share this answer
 
Comments
[no name] 13-Apr-20 11:52am    
I see, as ZipFile.ExtractToDirectory does not report progress by itself, could you suggest me an alternative for decompressing?
Dave Kreskowiak 13-Apr-20 12:18pm    
I don't know of any library that's going to report progress. If you want a progress bar with decompression, you're going to have to write your own code to handle the decompression.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900