此問題已解決。我的錯誤是,在我的第二塊代碼中,我使用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提取時應該不同嗎?提前致謝。
您需要在開始之前提取添加事件處理程序。另外,SaveProgress事件在保存期間觸發,還有一個不同的處理程序,ExtractProgress在提取過程中觸發。 [見參考例子用法](http://dotnetzip.herobo.com/DNZHelp/html/01318049-51bf-3876-e766-ad83e62fdcf5.htm) –
啊,愚蠢的錯誤在我的結尾。我將它改回到ExtractProgress(我之前有過,但是由於某種原因改回來了/),而我真正的問題是,正如你所說我在使用事件處理程序之前使用Zip.ExtractAll。謝謝!您能否將您的評論發佈爲答案,以便我可以將其標記爲已接受? –