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 ArchiveFileExample
// 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
| Name | Description |
|---|---|
| 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.csDetects the format of the data of a seekable stream, from its current position, and restores the position.
public static ArchiveFormatInfo DetectFormat(Stream stream)Parameters
streamStream- A seekable stream.
Returns
ArchiveFormatInfo The format of the archive and its compression.
DetectFormat(string)
ArchiveFile.csDetects the format of a file.
public static ArchiveFormatInfo DetectFormat(string path)Parameters
pathstring- The path of the file.
Returns
ArchiveFormatInfo The format of the archive and its compression.
ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)
ArchiveFile.csExtracts 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
sourceArchiveFileNamestring- The path of the archive, or of its first volume.
destinationDirectoryNamestring- The directory to extract to. It is created when it does not exist.
overwriteFilesbooltrueto replace the existing files;falseto throw anIOExceptionwhen a file exists.optionsArchiveReaderOptions?- The options to read the archive.
ExtractToDirectoryAsync(string, string, bool, ArchiveReaderOptions?, CancellationToken)
ArchiveFile.csExtracts 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
sourceArchiveFileNamestring- The path of the archive, or of its first volume.
destinationDirectoryNamestring- The directory to extract to. It is created when it does not exist.
overwriteFilesbooltrueto replace the existing files;falseto throw anIOExceptionwhen a file exists.optionsArchiveReaderOptions?- The options to read the archive.
cancellationTokenCancellationToken- 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.csOpens 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
streamStream- A seekable stream, positioned at the start of the archive.
optionsArchiveReaderOptions?- The options to read the archive.
leaveOpenbooltrueto 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.csOpens 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
pathstring- The path of the archive, or of its first volume.
optionsArchiveReaderOptions?- 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.