ArchiveFile Class

Opens an archive whatever its format (zip, 7z, RAR, tar, and tar compressed with gzip, bzip2, XZ, LZMA, lzip, Zstandard or Unix compress), detected from its content.

public static class ArchiveFile

Example

// The format is detected from the content: zip, 7z, RAR, or tar compressed with gzip, bzip2, XZ, ...
using (var archive = ArchiveFile.Open(path))
{
    foreach (var entry in archive.Entries)
    {
        if (entry.IsDirectory)
            continue;

        using var stream = entry.Open();
        using var reader = new StreamReader(stream);
        var text = reader.ReadToEnd();
        Assert.NotNull(text);
    }

    var hello = archive.GetEntry("hello.txt");
    Assert.Equal(16, hello?.Length);
}

ArchiveFile.ExtractToDirectory(path, destination);

Methods

NameDescription
DetectFormat(Stream)Detects the format of the data of a seekable stream, from its current position, and restores the position.
DetectFormat(string)Detects the format of a file.
ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)Extracts an archive file of any supported format to a directory.
ExtractToDirectoryAsync(string, string, bool, ArchiveReaderOptions?, CancellationToken)Extracts an archive file of any supported format to a directory.
Open(Stream, ArchiveReaderOptions?, bool)Opens an archive for random access from a seekable stream. A compressed tar archive is decompressed to a temporary file, deleted when the archive is disposed.
Open(string, ArchiveReaderOptions?)Opens an archive file for random access. The other volumes of a multi-volume archive are found next to it (see ArchiveReaderOptions.VolumeResolver). A compressed tar archive is decompressed to a temporary file, deleted when the archive is disposed.

DetectFormat overloads

DetectFormat(Stream)

ArchiveFile.cs

Detects the format of the data of a seekable stream, from its current position, and restores the position.

public static ArchiveFormatInfo DetectFormat(Stream stream)
Parameters
stream Stream
A seekable stream.
Returns

ArchiveFormatInfo The format of the archive and its compression.

DetectFormat(string)

ArchiveFile.cs

Detects the format of a file.

public static ArchiveFormatInfo DetectFormat(string path)
Parameters
path string
The path of the file.
Returns

ArchiveFormatInfo The format of the archive and its compression.

ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)

ArchiveFile.cs

Extracts an archive file of any supported format to a directory.

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

Parameters

sourceArchiveFileName string
The path of the archive, or of its first volume.
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 to read the archive.

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

ArchiveFile.cs

Extracts an archive file of any supported format to a directory.

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, or of its first volume.
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 to read the archive.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task A task that completes when every entry is extracted.

Open overloads

Open(Stream, ArchiveReaderOptions?, bool)

ArchiveFile.cs

Opens an archive for random access from a seekable stream. A compressed tar archive is decompressed to a temporary file, deleted when the archive is disposed.

public static Archive Open(Stream stream, ArchiveReaderOptions? options = null, bool leaveOpen = false)
Parameters
stream Stream
A seekable stream, positioned at the start of the archive.
options ArchiveReaderOptions?
The options to read the archive.
leaveOpen bool
true to leave the stream open when the archive is disposed.
Returns

Archive The archive.

Exceptions
InvalidDataException
The data are not an archive of a supported format, or they are corrupted.

Open(string, ArchiveReaderOptions?)

ArchiveFile.cs

Opens an archive file for random access. The other volumes of a multi-volume archive are found next to it (see ArchiveReaderOptions.VolumeResolver). A compressed tar archive is decompressed to a temporary file, deleted when the archive is disposed.

public static Archive Open(string path, ArchiveReaderOptions? options = null)
Parameters
path string
The path of the archive, or of its first volume.
options ArchiveReaderOptions?
The options to read the archive.
Returns

Archive The archive.

Exceptions
InvalidDataException
The file is not an archive of a supported format, or it is corrupted.