gpt4 book ai didi

c# - DotNetZip ExtractProgress 错误?

转载 作者:太空狗 更新时间:2023-10-29 20:31:52 25 4
gpt4 key购买 nike

ExtractProgressEventArgs.EntriesTotalExtractProgressEventArgs.EntriesExtracted 始终为零。这是一个已知的错误?请参阅下面的代码:

public static void UnZip(string zipFile, string destination)
{
using(ZipFile zip = ZipFile.Read(zipFile))
{
zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);

foreach (ZipEntry entry in zip)
{
entry.Extract(destination, ExtractExistingFileAction.OverwriteSilently);
}
Console.WriteLine("DONE");
}
}

static void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if(e.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
Console.WriteLine(String.Format("{0} : {1} / {2} = {3}%", e.CurrentEntry.FileName, e.EntriesTotal, e.EntriesExtracted, ((double)e.EntriesTotal / (double)e.EntriesExtracted) * 100.00));
}

最佳答案

定义的行为:

来自 http://cheeso.members.winisp.net/DotNetZipHelp/html/91d797c7-efff-99a3-2b14-6c9a9797b324.htm

EntriesExtracted Number of entries extracted so far. This is set only if the EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and the Extract() is occurring witin the scope of a call to ExtractAll().

原因很简单:如果您在自己控制的循环中调用 Extract(),则库无法知道您计划调用它多少次,您已经调用了多少次一行,是否将前 5 次与后 5 次一起计算,依此类推。所以 DotNetZip 无法为您提供有关您完成了多少的可靠信息。

您有两个简单的解决方法:

  • 使用一个 int 计数器,每次调用循环中的 Extract() 时该计数器都会递增。

  • 调用 ExtractAll(),在这种情况下,它将发出 ExtractProgressEvents,其中 EntriesExtracted 值设置为一个有意义的数字。

选项 1:

    int n;
using(ZipFile zip = ZipFile.Read(zipFile))
{
zip.ExtractProgress += zip_ExtractProgress;
n = 0;
foreach (ZipEntry entry in zip)
{
n++;
entry.Extract(destination, ExtractExistingFileAction.OverwriteSilently);
}
Console.WriteLine("DONE");
}

选项 2:

    using(ZipFile zip = ZipFile.Read(zipFile))
{
zip.ExtractProgress += zip_ExtractProgress;
zip.ExtractAll(destination,
ExtractExistingFileAction.OverwriteSilently);
Console.WriteLine("DONE");
}

关于c# - DotNetZip ExtractProgress 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6109370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com