How do I calculate or get the progress when decompressing a split RAR archive? #389

Closed
opened 2026-01-29 22:11:08 +00:00 by claunia · 1 comment
Owner

Originally created by @tardezyx on GitHub (Jan 6, 2020).

I decompress a single split RAR archive consisting of 10 rar-files with SharpCompress like this:

private static double percentage;
private static long totalSize;

public static void ExtractRAR(sourcePath, targetPath)
{
	DirectoryInfo dirInfo = new DirectoryInfo(sourcePath);
	List<FileInfo> files = dirInfo.GetFiles("*.rar").OrderBy(o => o.FullName).ToList();

	try
	{
		IArchive archive = ArchiveFactory.Open(files[0]);
		Extraction.totalSize = archive.TotalSize;

		foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory))
		{
			archive.CompressedBytesRead += Archive_CompressedBytesRead;
			entry.WriteToDirectory(targetPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
		}
	}
	catch (IOException ex)
	{
		MessageBox.Show("Error: " + ex.Message);
	}

	private static void Archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e)
	{
		Extraction.percentage = ((double)e.CompressedBytesRead / (double)Extraction.totalSize) * 100;
		MessageBox.Show(Extraction.percentage.ToString());
	}
}

It is successfully extracted but the output I get from the progress is more than confusing. It repeats a value of "10,9...", then come some "0" and inbetween a "21,8,...". It does not count further to "32,7" etc. What is wrong here!?

Originally created by @tardezyx on GitHub (Jan 6, 2020). I decompress a single split RAR archive consisting of 10 rar-files with SharpCompress like this: ``` private static double percentage; private static long totalSize; public static void ExtractRAR(sourcePath, targetPath) { DirectoryInfo dirInfo = new DirectoryInfo(sourcePath); List<FileInfo> files = dirInfo.GetFiles("*.rar").OrderBy(o => o.FullName).ToList(); try { IArchive archive = ArchiveFactory.Open(files[0]); Extraction.totalSize = archive.TotalSize; foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory)) { archive.CompressedBytesRead += Archive_CompressedBytesRead; entry.WriteToDirectory(targetPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } catch (IOException ex) { MessageBox.Show("Error: " + ex.Message); } private static void Archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e) { Extraction.percentage = ((double)e.CompressedBytesRead / (double)Extraction.totalSize) * 100; MessageBox.Show(Extraction.percentage.ToString()); } } ``` It is successfully extracted but the output I get from the progress is more than confusing. It repeats a value of "10,9...", then come some "0" and inbetween a "21,8,...". It does not count further to "32,7" etc. What is wrong here!?
claunia added the question label 2026-01-29 22:11:08 +00:00
Author
Owner

@tardezyx commented on GitHub (Jan 6, 2020):

Okay, got it:

I do not need to calculate the substreams (or whatever he is doing within archive.CompressedBytesRead) but the actual size of the files he is extracting.

Corrected code:

public static void ExtractRAR(sourcePath, targetPath)
{
	DirectoryInfo dirInfo = new DirectoryInfo(sourcePath);
	List<FileInfo> files = dirInfo.GetFiles("*.rar").OrderBy(o => o.FullName).ToList();

	try
	{
		IArchive archive = ArchiveFactory.Open(files[0]);
		double totalSize = archive.TotalSize;
		double proceededSize = 0;
		double percentage;

		foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory))
		{
			entry.WriteToDirectory(targetPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
			proceededSize += entry.Size;
			percentage = (proceededSize / totalSize) * 100;
		}
	}
	catch (IOException ex)
	{
		MessageBox.Show("Error: " + ex.Message);
	}
}

This "issue" can be closed :)

@tardezyx commented on GitHub (Jan 6, 2020): Okay, got it: I do not need to calculate the substreams (or whatever he is doing within `archive.CompressedBytesRead`) but the actual size of the files he is extracting. Corrected code: ``` public static void ExtractRAR(sourcePath, targetPath) { DirectoryInfo dirInfo = new DirectoryInfo(sourcePath); List<FileInfo> files = dirInfo.GetFiles("*.rar").OrderBy(o => o.FullName).ToList(); try { IArchive archive = ArchiveFactory.Open(files[0]); double totalSize = archive.TotalSize; double proceededSize = 0; double percentage; foreach (IArchiveEntry entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(targetPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); proceededSize += entry.Size; percentage = (proceededSize / totalSize) * 100; } } catch (IOException ex) { MessageBox.Show("Error: " + ex.Message); } } ``` This "issue" can be closed :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#389