使用Ionic.ZIP和进度条[Windows窗体]

时间:2019-06-04 12:46:50

标签: c# winforms progress-bar dotnetzip ionic-zip

我正在准备一个为给定目录创建zip文件的应用程序。

我希望在创建zip时显示以下内容。

  1. 完成该拉链的估计时间(经过的时间和剩余时间)
  2. 完成压缩的百分比

这是我的书面代码:

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) + "%";           
        }
    }

第一个进度条适用于文件的大小,因为 e.BytesTransferred e.TotalBytesToTransfer 的返回大小为 Bytes

但是 e.EntriesSaved e.EntriesTotal 将返回已保存条目的长度和我们添加的条目总数。

所以我希望第二个进度条能够根据“整个选定文件”和“压缩文件”的大小来工作。

您的努力将不胜感激。

谢谢...

0 个答案:

没有答案