Search code examples
c#progressrarsharpcompress

c# sharpcompress : How to check progress


IArchive rar = SharpCompress.Archive.Rar.RarArchive.Open(new FileInfo("ze.rar"), SharpCompress.Common.Options.None);
        rar.WriteToDirectory(Directory.GetCurrentDirectory() + "\\DATA", SharpCompress.Common.ExtractOptions.Overwrite);

With the above code i'm able to extract the rar file, however i would like to show the progress through the console. How can i check the progress?


Solution

  • This should provide a sample of how to calculate the current percentage of the extraction operation. Thanks to @MathiasRJessen for pointing out the behavior of IArchive.WriteToDirectory extension.

    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.
    }