1

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...

Hiren Patel
  • 1,071
  • 11
  • 34
  • 1
    Do you mean that e.EntriesSaved and e.EntriesTotal report the number of files processed and total, respectively? If so, you'll just want to compute the total number of bytes in the input files (use `FileInfo` for that) and keep the total number of bytes processed from already processed files. – mostanes Jun 06 '19 at 13:42
  • Thanks, @mostanes but it not feasible for me, because I try to add multiple folders so I need to calculate the length of all files in bytes. – Hiren Patel Jun 06 '19 at 14:27

0 Answers0