2016-08-15 35 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

互聯網檔案有一個捕獲好東西。更新了快照鏈接,並在此處複製了代碼以獲得更好的效果 –