2016-08-15 37 views
0

此问题已解决。我的错误是,在我的第二块代码中,我使用SaveProgress而不是ExtractProgress,并在设置Event Handler之前使用Zip.ExtractAll。感谢Bradley Moorfield帮助我。DotNetZip - 显示提取进度?

因此,我使用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,这d达到100%22次。这一切都工作完美,我喜欢我拥有它的方式,但是我在从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); 
          }; 
         } 

无论出于何种原因,该代码不打印任何百分比可言,所以我猜它有事情做与我实际计算百分比的方式。压缩vs提取时应该不同吗?提前致谢。

+1

您需要在开始之前提取添加事件处理程序。另外,SaveProgress事件在保存期间触发,还有一个不同的处理程序,ExtractProgress在提取过程中触发。 [见参考例子用法](http://dotnetzip.herobo.com/DNZHelp/html/01318049-51bf-3876-e766-ad83e62fdcf5.htm) –

+0

啊,愚蠢的错误在我的结尾。我将它改回到ExtractProgress(我之前有过,但是由于某种原因改回来了/),而我真正的问题是,正如你所说我在使用事件处理程序之前使用Zip.ExtractAll。谢谢!您能否将您的评论发布为答案,以便我可以将其标记为已接受? –

回答

1

您需要在开始提取之前添加事件处理程序。此外,在保存期间触发事件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); 
    } 
    } 
} 
+1

很好的答案。链接被破坏:) – Davlio

+1

互联网档案有一个捕获好东西。更新了快照链接,并在此处复制了代码以获得更好的效果 –