Hiren Patel
Hiren Patel

Reputation: 1157

Compress File Using Ionic.ZIP with Progress Bar [Windows Forms]

I'm preparing an application that creates a zip file for a given directory.

I want the following things to display while creating a zip.

  1. Estimated time for completing that zip (Elapsed Time and Time left)
  2. The percentage for completion of Zipping

Here is my written code:

enter image description here

    private void CreateZip(string FilePath)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AddProgress += Zip_AddProgress;
            zip.SaveProgress += Zip_SaveProgress;
            zip.CompressionMethod = Ionic.Zip.CompressionMethod.Deflate;
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
            if (!string.IsNullOrEmpty(FilePath))
            {
                zip.AddDirectory(FilePath, new DirectoryInfo(FilePath).Name);
            }
           var d= zip;

            if (File.Exists(txtDest.Text))
            {
                File.Delete(txtDest.Text);
            }
            zip.Save(txtDest.Text);
        }
    }

    private void Zip_SaveProgress(object sender, SaveProgressEventArgs e)
    {
        if (e.EventType == ZipProgressEventType.Saving_Started)
            lblFileName.Text = "Proccess Started Successfully";

        if (e.EventType == ZipProgressEventType.Saving_AfterSaveTempArchive)
            lblFileName.Text = "Proccess Completed Successfully";


        if (e.BytesTransferred > 0 && e.TotalBytesToTransfer > 0)
        {
            int progress = (int)Math.Floor((decimal)((e.BytesTransferred * 100) / e.TotalBytesToTransfer));
            pbPerFile.Value = progress;
            lblPercentagePerFile.Text = Convert.ToString(progress) + "%";
            Application.DoEvents();
        }

        if (e.EntriesSaved > 0 && e.EntriesTotal > 0)
        {
            int progress = (int)Math.Floor((decimal)((e.EntriesSaved * 100) / e.EntriesTotal));
            pbTotalFile.Value = progress;
            Application.DoEvents();
            lblTotal.Text = Convert.ToString(progress) + "%";           
        }
    }

The first progress bar works on the size of the file because of e.BytesTransferred and e.TotalBytesToTransfer is return size in Bytes.

But e.EntriesSaved and e.EntriesTotal will return the length of the saved entries and total number of entries that we have added.

So I want that second Progress Bar to work based on the size of the Entire Selected files and Compressed File.

Your efforts will be appreciated.

Thanks...

Upvotes: 1

Views: 1562

Answers (0)

Related Questions