c#sharpcompress:如何检查进度

时间:2016-04-17 20:57:06

标签: c# progress rar sharpcompress

String string = "ElasticSearch-1.0-20160417";
String[] parts = string.split("-");
String projectName = parts[0]; // ElasticSearch
String versionNumber= parts[1]; // 1.0
String downloadDate= parts[2]; // 20160417

使用上面的代码,我能够提取rar文件,但是我希望通过控制台显示进度。我该如何检查进度?

2 个答案:

答案 0 :(得分:1)

这应提供如何计算提取操作的当前百分比的示例。感谢@MathiasRJessen指出IArchive.WriteToDirectory扩展名的行为。

IArchive rar = SharpCompress.Archive.Rar.RarArchive.Open(new FileInfo("ze.rar"), SharpCompress.Common.Options.None);
string directory = Path.Combine(Directory.GetCurrentDirectory(), "DATA");

// Calculate the total extraction size.
double totalSize = rar.Entries.Where(e => !e.IsDirectory).Sum(e => e.Size);
long completed = 0;

// Use the same logic for extracting each file like IArchive.WriteToDirectory extension.
foreach (var entry in rar.Entries.Where(e => !e.IsDirectory))
{
    entry.WriteToDirectory(directory, ExtractOptions.Overwrite);

    // Track each individual extraction.
    completed += entry.Size;
    var percentage = completed / totalSize;
    // TODO do something with percentage.
}

答案 1 :(得分:1)

实际上,我发现此代码比答案更有效。

CP_UTF8

这是从我的解压缩类中获取的,因此逻辑中包含其他信息。但是,重要的是将'CompressedBytesRead'强制转换为两倍,然后将其划分为档案的总大小,也应强制转换为两倍。

`

Well, this was not properly documented anywhere on the Internet. There were some possibilities, but they wouldn't calculate properly. This is a working Progress percentage calculation for decompressing archives in SharpCompress.

`

如果有人有更好的东西,我会全力以赴,但这将有助于实现进度条。