Proper Decompression Progress Calculation #362

Open
opened 2026-01-29 22:10:40 +00:00 by claunia · 2 comments
Owner

Originally created by @chefbennyj1 on GitHub (Jul 20, 2019).

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.

This is taken from my Decompression Class, so there is extra information in the logic. However, what is important is casting the 'CompressedBytesRead' to double, and dividing it into the total size of the archive which should also be cast to double.

`

    public static double Percentage {get; set;}

    public static long totalSize { get; set; }

    public static void BeginDecompression(string fullFileName, string fileName)
    {
        try
        {               

            var settings = new Configuration().GetSettings();

            CurrentExtractionName = (Path.GetFileNameWithoutExtension(fileName)); 
          

            StringHelpers.ItemInfo item = StringHelpers.GetItemInfo(fileName);

            string extractPath = settings.EmbyAutoOrganizeFolderPath + "\\" +
                                 (Path.GetFileNameWithoutExtension(fileName));
            Directory.CreateDirectory(extractPath);
            IArchive archive = ArchiveFactory.Open(fullFileName);

            // Calculate the total extraction size.
            totalSize = archive.TotalSize;
            
            Console.WriteLine(totalSize);

            foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory))
            {
                archive.EntryExtractionEnd += FileMoveSuccess; 
                archive.CompressedBytesRead += Archive_CompressedBytesRead;
                entry.WriteToDirectory(extractPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
      
            }
            
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }

    private static void Archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e)
    {            
        Percentage = ((double)e.CompressedBytesRead / (double)totalSize) * 100;

        Console.WriteLine(Percentage);
    }

`

If anyone has something better, I'm all ears, but this will work to implement a progress bar.

Originally created by @chefbennyj1 on GitHub (Jul 20, 2019). 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. This is taken from my Decompression Class, so there is extra information in the logic. However, what is important is casting the 'CompressedBytesRead' to double, and dividing it into the total size of the archive which should also be cast to double. ` public static double Percentage {get; set;} public static long totalSize { get; set; } public static void BeginDecompression(string fullFileName, string fileName) { try { var settings = new Configuration().GetSettings(); CurrentExtractionName = (Path.GetFileNameWithoutExtension(fileName)); StringHelpers.ItemInfo item = StringHelpers.GetItemInfo(fileName); string extractPath = settings.EmbyAutoOrganizeFolderPath + "\\" + (Path.GetFileNameWithoutExtension(fileName)); Directory.CreateDirectory(extractPath); IArchive archive = ArchiveFactory.Open(fullFileName); // Calculate the total extraction size. totalSize = archive.TotalSize; Console.WriteLine(totalSize); foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory)) { archive.EntryExtractionEnd += FileMoveSuccess; archive.CompressedBytesRead += Archive_CompressedBytesRead; entry.WriteToDirectory(extractPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } private static void Archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e) { Percentage = ((double)e.CompressedBytesRead / (double)totalSize) * 100; Console.WriteLine(Percentage); } ` If anyone has something better, I'm all ears, but this will work to implement a progress bar.
claunia added the enhancement label 2026-01-29 22:10:41 +00:00
Author
Owner

@adamhathcock commented on GitHub (Aug 20, 2019):

Probably need to rethink how progress is reported but this uses the hooks I have as intended.

@adamhathcock commented on GitHub (Aug 20, 2019): Probably need to rethink how progress is reported but this uses the hooks I have as intended.
Author
Owner

@DineshSolanki commented on GitHub (Jul 19, 2024):

How do I get the total progress, this only provides progress for individual entries

@DineshSolanki commented on GitHub (Jul 19, 2024): How do I get the total progress, this only provides progress for individual entries
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#362