简体   繁体   English

DotNetZip - 显示提取进度?

[英]DotNetZip - Display progress of extraction?

This problem is solved. 这个问题解决了。 My mistake was that within my second block of code I was using SaveProgress instead of ExtractProgress and I was using Zip.ExtractAll before setting up the Event Handler. 我的错误是在我的第二个代码块中我使用的是SaveProgress而不是ExtractProgress,我在设置事件处理程序之前使用了Zip.ExtractAll。 Thanks to Bradley Moorfield for helping me. 感谢Bradley Moorfield帮助我。

So I'm using the DotNetZip library to compress/extract zip files throughout my code. 所以我使用DotNetZip库在我的代码中压缩/提取zip文件。 I was able to accomplish showing the percentage of compression with this code: 我能够用这段代码完成显示压缩百分比:

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

The reason it is like this is beacuse it compresses 100 mb at a time, so for example, if a folder was 2153 mb, it'd go to 100% 22 times. 它是这样的原因是因为它一次压缩100 mb,所以例如,如果一个文件夹是2153 mb,它将达到100%22次。 This is all working perfectly, and I like the way I have it, however I'm having some trouble displaying the percentage done when extracting from a zip to a directory. 这一切都很完美,我喜欢我的方式,但是我在从zip解压缩到目录时显示完成百分比时遇到了一些麻烦。 Here's my code so far: 到目前为止,这是我的代码:

                        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);
                            };
                        }

For whatever reason, this code doesn't print any percentage at all, so I'm guessing it's got something to do with the way I'm actually calculating the percentage. 无论出于何种原因,这段代码根本不打印任何百分比,所以我猜它与我实际计算百分比的方式有关。 Should it be different when compressing vs extracting? 压缩与提取时应该不同吗? Thanks in advance. 提前致谢。

You need to add the event handler before you start extracting. 您需要在开始提取之前添加事件处理程序。 Also, the SaveProgress event fires during saving, there is a different handler, ExtractProgress that fires during extraction. 此外, SaveProgress事件在保存期间触发,还有一个不同的处理程序, ExtractProgress在提取期间触发。

See the reference for example usage (web.archive.org) 请参阅参考用例(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);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM