adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 479 forks source link

Proper Decompression Progress Calculation #462

Open chefbennyj1 opened 5 years ago

chefbennyj1 commented 5 years ago

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.

adamhathcock commented 5 years ago

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

DineshSolanki commented 1 month ago

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