List RAR 5 members and copy one stored (uncompressed) file. Compressed and encrypted data are not supported.
dotnet add package RecursiveAI.OneArchiveRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
stored.rar is in the example download; it contains a stored hello.txt.
using RecursiveAI.OneArchive; using RecursiveAI.OneArchive.Rar; using var file = File.OpenRead("stored.rar"); using var reader = new RarReader(file); while (reader.GetNextEntry() is { } entry) { Console.WriteLine(entry.Name); if (entry.Name != "hello.txt") continue; using var data = reader.OpenEntryStream(); using var output = File.Create("hello.txt"); data.CopyTo(output); }
hello.txt docs/sub/notes.txt empty.txt random.bin ünï-cødé-文件-🙂.txt link-to-hello escaping-link hard-to-hello docs/sub docs empty-dir
Unzip the example, then dotnet run.
The full program
stored.rar comes in the download from the public repository’s generated sample. Its hello.txt contains Hello, RAR5 container! plus a newline, repeated 40 times. The program lists all names but extracts only that file.
using RecursiveAI.OneArchive; using RecursiveAI.OneArchive.Rar; using var file = File.OpenRead("stored.rar"); using var reader = new RarReader(file); while (reader.GetNextEntry() is { } entry) { Console.WriteLine(entry.Name); if (entry.Name != "hello.txt") continue; using var data = reader.OpenEntryStream(); using var output = File.Create("hello.txt"); data.CopyTo(output); } Console.WriteLine($"Extracted hello.txt: {new FileInfo("hello.txt").Length} bytes"); Console.WriteLine(File.ReadLines("hello.txt").First());
dotnet run prints:
hello.txt docs/sub/notes.txt empty.txt random.bin ünï-cødé-文件-🙂.txt link-to-hello escaping-link hard-to-hello docs/sub docs empty-dir Extracted hello.txt: 920 bytes Hello, RAR5 container!
Opening compressed or encrypted entry data throws RarUnsupportedException; this is not a general RAR decompressor.
Variations
Inspect the method of the selected entry without opening its data. Method 0 means stored.
using RecursiveAI.OneArchive.Rar; using var file = File.OpenRead("stored.rar"); using var reader = new RarReader(file); while (reader.GetNextEntry() is RarEntry entry) { if (entry.Name != "hello.txt") continue; Console.WriteLine( $"{entry.Name}: method {entry.Method}"); }
hello.txt: method 0Related recipes
The short answer
Use RarReader to walk RAR 5 headers with GetNextEntry. It lists compressed entries as well as stored ones, but OpenEntryStream supports only unencrypted stored data. The example selects the known hello.txt member and copies it to a fixed local filename.
The download includes the input, so the program runs without a local archiving tool. The fixture is generated by the pinned public repository’s sample generator. Its other members show names, directories and links; listing them does not extract them.
How do I list RAR files in C#?
Open the archive as a stream and pass it to RarReader. Each GetNextEntry advances to the next member in file order. Read the member’s Name, Kind and Size for a basic inventory, or use RarEntry for RAR-specific fields. The variation above prints Method without opening any member content.
Listing and decoding are separate operations. According to the RAR reader documentation, compressed methods 1 through 5 can be listed fully; opening their data raises RarUnsupportedException. The exception identifies the member, method and dictionary size. Method 0 is the stored method, but encrypted stored data remains unsupported. Check IsEncrypted as well as the method when deciding whether your application can offer extraction.
A filename ending in .rar is not enough to decide compatibility. RAR 4 is detected and refused, and archives with encrypted headers are refused before their names can be listed. Those errors have distinct RarUnsupportedKind values, allowing an application to explain why an inventory is unavailable rather than reporting a successful empty archive.
How do I extract files from a .RAR file?
For an unencrypted stored entry, call OpenEntryStream while that entry is current and copy it to the destination. Read to the end so that the default checksum verification runs. The full program does that for hello.txt, then prints its extracted size and first line.
The destination in this example is deliberately fixed. It never joins arbitrary archive member names onto an output directory. That distinction matters for a fixture that also contains link entries: an inventory is not an instruction to recreate every path or follow every link. For general directory extraction, use Archive.Extract rather than turning this selection loop into unchecked filesystem writes.
CRC-32 and, when present, BLAKE2sp are checked as stored content reaches its end. A mismatch raises CompressionFormatException. A copied destination may already contain bytes at that point; checksum verification is not a transactional promise that no output was written. If your application needs an all-or-nothing result, stage its output before publishing it.
Multipart archives need an explicit next-volume provider through RarReaderOptions.Volumes. The reader does not infer a complete volume set from the first filename. This page’s single-volume sample needs no provider. Neither supplying volumes nor selecting a stored entry adds support for RAR compression or encryption.