RarArchive.RarArchive

RarArchive(string, RarArchiveLoadOptions)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

public RarArchive(string path, RarArchiveLoadOptions loadOptions = null)
ParameterTypeDescription
pathStringThe fully qualified or the relative path to the archive file.
loadOptionsRarArchiveLoadOptionsOptions to load existing archive with.

Exceptions

exceptioncondition
ArgumentNullExceptionpath is null.
SecurityExceptionThe caller does not have the required permission to access.
ArgumentExceptionThe path is empty, contains only white spaces, or contains invalid characters.
UnauthorizedAccessExceptionAccess to file path is denied.
PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
NotSupportedExceptionFile at path contains a colon (:) in the middle of the string.
FileNotFoundExceptionThe file is not found.
DirectoryNotFoundExceptionThe specified path is invalid, such as being on an unmapped drive.
IOExceptionThe file is already open.

Remarks

This constructor does not decompress any entry. See Open method for decompressing.

Examples

The following example extract an archive, then decompress first entry to a MemoryStream.

var extracted = new MemoryStream();
using (RarArchive archive = new RarArchive("data.rar"))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

See Also


RarArchive(Stream, RarArchiveLoadOptions)

Initializes a new instance of the RarArchive class and composes entries list can be extracted from the archive.

public RarArchive(Stream sourceStream, RarArchiveLoadOptions loadOptions = null)
ParameterTypeDescription
sourceStreamStreamThe source of the archive.
loadOptionsRarArchiveLoadOptionsOptions to load existing archive with.

Exceptions

exceptioncondition
ArgumentExceptionsourceStream is not seekable.
InvalidDataExceptionWrong signature for archive. - or - The file is not a RAR archive.
InvalidOperationException

Remarks

This constructor does not decompress any entry. See Open method for decompressing.

Examples

The following example decipher and decompress first entry to a MemoryStream.

var fs = File.OpenRead("encrypted.rar");
var extracted = new MemoryStream();
using (RarArchive archive = new RarArchive(fs, new RarArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

See Also