RarFile Class

Opens and extracts the RAR archives of the file system, finding the volumes of a multi-volume archive next to the first one.

public static class RarFile

Methods

NameDescription
ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)Extracts the entries of a RAR archive to a directory. An entry whose path leaves the directory (.., a rooted path, or a symbolic link that points outside of it) is rejected with an IOException.
ExtractToDirectoryAsync(string, string, bool, ArchiveReaderOptions?, CancellationToken)Extracts the entries of a RAR archive to a directory. An entry whose path leaves the directory (.., a rooted path, or a symbolic link that points outside of it) is rejected with an IOException.
OpenRead(string, ArchiveReaderOptions?)Opens a RAR archive. When ArchiveReaderOptions.VolumeResolver is null, the volumes of a multi-volume archive are found next to it: name.part2.rar, name.part3.rar, ... (keeping the number of digits of the first volume, as in name.part01.rar), or name.r00, name.r01, ... for the volumes of the old naming scheme that follow name.rar.

ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)

RarFile.cs

Extracts the entries of a RAR archive to a directory. An entry whose path leaves the directory (.., a rooted path, or a symbolic link that points outside of it) is rejected with an IOException.

public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, bool overwriteFiles = false, ArchiveReaderOptions? options = null)

Parameters

sourceArchiveFileName string
The path of the archive, the first volume of a multi-volume archive.
destinationDirectoryName string
The directory to extract to. It is created when it does not exist.
overwriteFiles bool
true to replace the existing files; false to throw an IOException when a file exists.
options ArchiveReaderOptions?
The options, or null for the defaults.

Example

RarFile.ExtractToDirectory(archivePath, destination, overwriteFiles: true);
Assert.True(File.Exists(Path.Combine(destination, "sub/dir1/file1.txt")));

ExtractToDirectoryAsync(string, string, bool, ArchiveReaderOptions?, CancellationToken)

RarFile.cs

Extracts the entries of a RAR archive to a directory. An entry whose path leaves the directory (.., a rooted path, or a symbolic link that points outside of it) is rejected with an IOException.

net10.0

public static Task ExtractToDirectoryAsync(string sourceArchiveFileName, string destinationDirectoryName, bool overwriteFiles = false, ArchiveReaderOptions? options = null, CancellationToken cancellationToken = default)

net11.0

public static Task ExtractToDirectoryAsync(string sourceArchiveFileName, string destinationDirectoryName, bool overwriteFiles = false, ArchiveReaderOptions? options = null, CancellationToken cancellationToken = default)

Parameters

sourceArchiveFileName string
The path of the archive, the first volume of a multi-volume archive.
destinationDirectoryName string
The directory to extract to. It is created when it does not exist.
overwriteFiles bool
true to replace the existing files; false to throw an IOException when a file exists.
options ArchiveReaderOptions?
The options, or null for the defaults.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task A task that completes when every entry is extracted.

OpenRead(string, ArchiveReaderOptions?)

RarFile.cs

Opens a RAR archive. When ArchiveReaderOptions.VolumeResolver is null, the volumes of a multi-volume archive are found next to it: name.part2.rar, name.part3.rar, ... (keeping the number of digits of the first volume, as in name.part01.rar), or name.r00, name.r01, ... for the volumes of the old naming scheme that follow name.rar.

public static RarArchive OpenRead(string path, ArchiveReaderOptions? options = null)

Parameters

path string
The path of the archive, the first volume of a multi-volume archive.
options ArchiveReaderOptions?
The options, or null for the defaults.

Returns

RarArchive The archive, which owns the files it opens.

Exceptions

InvalidDataException
The file is not a RAR archive, or the archive is corrupted.
ArchivePasswordException
The headers are encrypted and the password is missing or wrong.