c# - DotNetZip - 显示提取进度?

标签 c# .net zip progress dotnetzip

这个问题已经解决了。我的错误是,在第二个代码块中,我使用了 SaveProgress 而不是 ExtractProgress,并且在设置事件处理程序之前使用了 Zip.ExtractAll。感谢Bradley Moorfield帮助我。

因此,我在整个代码中使用 DotNetZip 库来压缩/提取 zip 文件。我能够使用以下代码完成显示压缩百分比:

                using (ZipFile zip = new ZipFile())
                {
                    zip.AddDirectory(filePath);
                    var mb = GetDirectorySize(filePath) / 1048576;
                    long timesRunNeeded = mb / 100;
                    if (mb % 100 > 0) { timesRunNeeded++; }
                    if (mb <= 100) { timesRunNeeded = 1; }
                    int timesRun = 1;
                    int pastP = 0;
                    zip.SaveProgress += (o, args) =>
                    {
                        var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
                        if (pastP != percentage)
                        {
                            clearLine();
                            setColor(ConsoleColor.Gray); Console.Write("   Percentage: "); setColor(ConsoleColor.Green);
                            Console.Write(percentage + " [" + timesRun + "/" + timesRunNeeded + "]");
                            if ((percentage == 100) && (pastP >= 0) && (pastP <= 99))
                            {
                                timesRun++;
                            }
                        }
                        pastP = percentage;
                    };
                    zip.Save(filePath + ".zip");
                }

之所以是这样,是因为它一次压缩 100 mb,所以例如,如果一个文件夹有 2153 mb,它会压缩 22 次到 100%。这一切都工作得很好,我喜欢我的方式,但是当我从 zip 提取到目录时,在显示完成的百分比时遇到了一些问题。 到目前为止,这是我的代码:

                        using (ZipFile zip = ZipFile.Read(filePath))
                        {
                            Directory.CreateDirectory(filePath.Replace(".zip", ""));
                            zip.ExtractAll(filePath.Replace(".zip", ""), ExtractExistingFileAction.OverwriteSilently);
                            zip.SaveProgress += (o, args) =>
                            { //Fix this, not showing percentage of extraction.
                                var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
                                clearLine();
                                Console.Write(percentage);
                            };
                        }

无论出于何种原因,这段代码根本不打印任何百分比,所以我猜测这与我实际计算百分比的方式有关。压缩与提取时应该有所不同吗?提前致谢。

最佳答案

您需要在开始提取之前添加事件处理程序。此外,SaveProgress 事件在保存期间触发,还有一个不同的处理程序 ExtractProgress 在提取期间触发。

See the reference for example usage (web.archive.org)

private static bool justHadByteUpdate = false;
public static void ExtractProgress(object sender, ExtractProgressEventArgs e)
{
  if(e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
  {
    if (justHadByteUpdate)
      Console.SetCursorPosition(0, Console.CursorTop);

    Console.Write("   {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer,
              e.BytesTransferred / (0.01 * e.TotalBytesToTransfer ));
    justHadByteUpdate = true;
  }
  else if(e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
  {
    if (justHadByteUpdate)
      Console.WriteLine();
    Console.WriteLine("Extracting: {0}", e.CurrentEntry.FileName);
    justHadByteUpdate= false;
  }
}

public static ExtractZip(string zipToExtract, string directory)
{
  string TargetDirectory= "extract";
  using (var zip = ZipFile.Read(zipToExtract)) {
    zip.ExtractProgress += ExtractProgress;
    foreach (var e in zip1)
    {
      e.Extract(TargetDirectory, true);
    }
  }
}

关于c# - DotNetZip - 显示提取进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948801/

相关文章:

c# - 如何向 Josh Smith 的 MVVM msdn 设计添加过滤

c# - 在不创建新实例的情况下引用其他类方法

.net - 如何管理每个开发人员的配置设置

python - 压缩并服务器多个内存中文件

java - 如何控制 Java 中的文件压缩参数以更快地解压缩对象?

c# - #if Debug 如果不调试则不会禁用按钮

.net - Visual Studio 2012 Express 和 .NET 2.0

c# - 使用 DataTable.Load() 而不创建所有列属性

java - 从 SOAP 附件中提取的 ZIP 文件损坏

c# - 在 select new 中使用 List<> 加入 LINQ