Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have done an windows app for extracting files, while extracting am showing the progress bar with particular extracting file name. now i have problem with progress percentage, percentage is calculating for each file(like for one file extraction 1....100% and again for another file extraction 1....100%) here now i want to show the progress percentage totally once only 1----100% with this my progress bar also jumping from starting after every file extraction. Progress and percentage should be show for whole zip file extraction.

can any one help me?

here is my code:


C#
public partial class progress : Form
    {
        private BackgroundWorker _zipBGWorker;
        private delegate void ZipProgressEventHandler(ExtractProgressEventArgs e);
        private delegate void ExtractEntryProgressEventHandler(ExtractProgressEventArgs e);
        private bool AllowClose = false;

        public progress()
        {
            InitializeComponent();
            SetTitle();
        }
        private void SetTitle()
        {
            this.Text = "Extracting Files";
        }
        public string outputpath; public string path; public string name; List<string> filenames = new List<string>(); int i = 0; string x;
        private void progress_Load(object sender, EventArgs e)
        {
            object[] args=new object[3];
            args[0] = path; args[1] = outputpath;//getting path and outputpath from another page
            _zipBGWorker = new BackgroundWorker();
            _zipBGWorker.WorkerSupportsCancellation = true;
            _zipBGWorker.WorkerReportsProgress = false;
            _zipBGWorker.DoWork += new DoWorkEventHandler(_zipBGWorker_DoWork);
            _zipBGWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_zipBGWorker_RunWorkerCompleted);
            _zipBGWorker.RunWorkerAsync(args);
            this.Cursor = Cursors.WaitCursor;

            label1.Text = "Extracting from "+name+" To "+outputpath;

        }
        void _zipBGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
            zipfile z = new zipfile();
            z.Close();
            MessageBox.Show("Extraction Completed");
        }

        private void _zipBGWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = (object[])e.Argument;
            var list = args.Cast<string>().ToList();
            string path1 = list[0];
            string op = list[1];
            //UpdateLabel((string)e.Argument);
            try
            {
                using (ZipFile zip = ZipFile.Read(path1))
                {
                    foreach (ZipEntry zfe in zip)
                    {
                        string n = zfe.FileName;
                        filenames.Add(n);
                    }
                    SetProgressBarMax(zip.Entries.Count);
                    zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
                    zip.ExtractAll(op, ExtractExistingFileAction.OverwriteSilently);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("There's been a problem extracting that zip file.  {0}", ex.Message),
                       "Error Extracting", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }
        void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
        {
            if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
            {
                StepEntryProgress(e);
            }
        }

        private void SetProgressBarMax(int n)
        {
            if (progressBar1.InvokeRequired)
            {
                progressBar1.Invoke(new Action<int>(SetProgressBarMax), new object[] { n });
            }
            else
            {
                progressBar1.Maximum = n;
                progressBar1.Step = 1;
            }
        }
        int f = 0;
        private void StepEntryProgress(ExtractProgressEventArgs e)
        {

            if (progressBar1.InvokeRequired)
            {
                progressBar1.Invoke(new ExtractEntryProgressEventHandler(this.StepEntryProgress), new object[] { e });

            }
            else
            {
                progressBar1.Style = ProgressBarStyle.Marquee;
                progressBar1.Maximum = 100;
                progressBar1.Value = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
                int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) / (double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
                updatepercent(percent.ToString());

                //DateTime time = DateTime.Now;

                //If endTime.Subtract(startTime).TotalSeconds > 60 then
                //                lblFileName.Text = "Complete. This operation took " & _
                //                    endTime.Subtract(startTime).Minutes.ToString() & _
                //                   " minutes, and " & endTime.Subtract(startTime).Seconds.ToString() & " seconds."
                f++;
                if (f>filenames.Count)
                {
                    if (i < filenames.Count)
                    {
                        x = filenames[i];
                        UpdateLabel(x);
                        int n = filenames.Count - i;
                        Updatefiles(n.ToString());
                        i++;
                    }
                }
            }

        }
        void UpdateLabel(string s)
        {
            if (this.label1.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                this.BeginInvoke (new MethodInvoker(() => UpdateLabel(s)));
            }
            else
            {
                // It's on the same thread, no need for Invoke
                this.label2.Text = s;
            }
        }
        void Updatefiles(string s)
        {
            if (this.label3.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                this.BeginInvoke(new MethodInvoker(() => UpdateLabel(s)));
            }
            else
            {
                // It's on the same thread, no need for Invoke
                this.label3.Text = "Remaining items "+s;
            }
        }
        void updatepercent(string s)
        {
            if (this.label4.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                this.BeginInvoke(new MethodInvoker(() => UpdateLabel(s)));
            }
            else
            {
                // It's on the same thread, no need for Invoke
                this.label4.Text = s +"%";
            }
        }

        public void ForceClose()
        {
            AllowClose = true;
            this.Close();
            AllowClose = false;
        }

        private void progress_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && AllowClose)
            {
                e.Cancel = true;
            }
        }
    }
Posted
Updated 9-Apr-14 1:53am
v3

define an variable:
C#
int lastBytesTransferred =0;


you can get total size of all file`s in zip file and then :
C#
progressBar1.Maximum = total size;
progressBar1.Value += e.BytesTransferred - lastBytesTransferred;
lastBytesTransferred = e.BytesTransferred;


Attention:
when a file was extracted set:
C#
lastBytesTransferred = 0;
 
Share this answer
 
Comments
Raajkumar.b 10-Apr-14 1:20am    
Still having same problem my Progress bar is jumping for every file.
saber rezaii magham 10-Apr-14 16:03pm    
what library file do you use for zipping it?
Raajkumar.b 14-Apr-14 1:38am    
using Ionic.Zip;
saber rezaii magham 14-Apr-14 2:50am    
Ok!
I try to send you a source cod.
You should use progresschanged event of backgroundworker imo, then you can do your progressbar update. Else you cause "Cross Thread" exception.
 
Share this answer
 
v2
C#
    int fileCount = 0;
    long totalSize = 0, total = 0, lastVal=0,sum=0;

    public void ReadAndExtract(string openPath, string savePath)
    {
        try
        {
            fileCount = 0;
            Ionic.Zip.ZipFile myZip = new Ionic.Zip.ZipFile();
            myZip = Ionic.Zip.ZipFile.Read(openPath);
            foreach (var entry in myZip)
            {
                fileCount++;
                totalSize += entry.UncompressedSize;
            }
            progressBar1.Maximum = (Int32)totalSize;
            myZip.ExtractProgress += new EventHandler<ionic.zip.extractprogresseventargs>(myZip_ExtractProgress);
            myZip.ExtractAll(savePath,Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }

    void myZip_ExtractProgress(object sender, Ionic.Zip.ExtractProgressEventArgs e)
    {

        System.Windows.Forms.Application.DoEvents();
        if (total != e.TotalBytesToTransfer)
        {
            sum += total - lastVal + e.BytesTransferred;
            total = e.TotalBytesToTransfer;
        }
        else
            sum += e.BytesTransferred - lastVal;

        lastVal = e.BytesTransferred;

        progressBar1.Value = (Int32)sum;
    }
</ionic.zip.extractprogresseventargs>
 
Share this answer
 

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