Abort uncompress multiple rar files #329

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

Originally created by @jsassner on GitHub (Sep 30, 2018).

Say you have a very big rar files (many multiple files) that you want to abort in the middle of an uncompress, how would you do that? I couldn't find anything in the code base that supports that.

The FilePartExtractionBegin event could have a property in FilePartExtractionBeginEventArgs where you could mark it as need to abort.

Originally created by @jsassner on GitHub (Sep 30, 2018). Say you have a very big rar files (many multiple files) that you want to abort in the middle of an uncompress, how would you do that? I couldn't find anything in the code base that supports that. The FilePartExtractionBegin event could have a property in FilePartExtractionBeginEventArgs where you could mark it as need to abort.
Author
Owner

@adamhathcock commented on GitHub (Oct 1, 2018):

So you're using a RarReader and doing MoveNext or something and want to stop mid-decompression of an Entry or just stop after extracting an entry?

@adamhathcock commented on GitHub (Oct 1, 2018): So you're using a RarReader and doing `MoveNext` or something and want to stop mid-decompression of an Entry or just stop after extracting an entry?
Author
Owner

@jsassner commented on GitHub (Oct 1, 2018):

I fixed it, thanks for the pointer in the correct direction.

This was the code i used before, and basically tried to abort in the FilePartExtractionBegin event.

using (var archive = RarArchive.Open(sourcePath)) {
	foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) {
		entry.WriteToDirectory(destinationPath, new ExtractionOptions {
			ExtractFullPath = false,
			Overwrite = true
		});
	}
}

Using your info, i ended up with:

using (RarReader reader = RarReader.Open(sourceFiles.Select(s => Path.Combine(sourcePath, s)).Select(p => File.OpenRead(p)))) {

	while (reader.MoveToNextEntry()) {
		if (!reader.Entry.IsDirectory) {
			using (var entryStream = reader.OpenEntryStream()) {
				string file = Path.GetFileName(reader.Entry.Key);
				if (null != file) {
					string destinationFileName = Path.Combine(destinationPath, file);
					using (var fs = File.OpenWrite(destinationFileName)) {
						TransferTo(reader, entryStream, fs);
					}
				}
			}
		}
	}
}

public long TransferTo(RarReader reader, Stream source, Stream destination) {
	var array = new byte[81920];
	long total = 0;
	int count;
	while ((count = source.Read(array, 0, array.Length)) != 0) {
		total += count;
		destination.Write(array, 0, count);
		if (total > 5000000) {
			// Just as a test
			reader.Cancel();
			return -1;
		}
	}

	return total;
}

This "issue" can now be closed.

@jsassner commented on GitHub (Oct 1, 2018): I fixed it, thanks for the pointer in the correct direction. This was the code i used before, and basically tried to abort in the FilePartExtractionBegin event. ``` using (var archive = RarArchive.Open(sourcePath)) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(destinationPath, new ExtractionOptions { ExtractFullPath = false, Overwrite = true }); } } ``` Using your info, i ended up with: ``` using (RarReader reader = RarReader.Open(sourceFiles.Select(s => Path.Combine(sourcePath, s)).Select(p => File.OpenRead(p)))) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { using (var entryStream = reader.OpenEntryStream()) { string file = Path.GetFileName(reader.Entry.Key); if (null != file) { string destinationFileName = Path.Combine(destinationPath, file); using (var fs = File.OpenWrite(destinationFileName)) { TransferTo(reader, entryStream, fs); } } } } } } public long TransferTo(RarReader reader, Stream source, Stream destination) { var array = new byte[81920]; long total = 0; int count; while ((count = source.Read(array, 0, array.Length)) != 0) { total += count; destination.Write(array, 0, count); if (total > 5000000) { // Just as a test reader.Cancel(); return -1; } } return total; } ``` This "issue" can now be closed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#329