SevenZipFile Class

Opens, creates and extracts 7z archives from paths, as ZipFile.

public static class SevenZipFile

Methods

NameDescription
CreateFromDirectory(string, string, SevenZipCompressionOptions?, bool)Creates an archive from the files and the directories of a directory. The symbolic links are stored as links, as the -snl switch of 7-Zip.
CreateFromDirectoryAsync(string, string, SevenZipCompressionOptions?, bool, CancellationToken)Creates an archive from the files and the directories of a directory. The symbolic links are stored as links, as the -snl switch of 7-Zip.
ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)Extracts an 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 an 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.
Open(string, SevenZipArchiveMode, ArchiveReaderOptions?, SevenZipCompressionOptions?)Opens an archive in a mode. A path that ends with .001 opens a multi-volume archive in SevenZipArchiveMode.Read mode.
OpenRead(string, ArchiveReaderOptions?)Opens an archive to read it. A path that ends with .001 opens a multi-volume archive: the volumes .002, .003, ... are found next to it, unless ArchiveReaderOptions.VolumeResolver is set.

CreateFromDirectory(string, string, SevenZipCompressionOptions?, bool)

SevenZipFile.cs

Creates an archive from the files and the directories of a directory. The symbolic links are stored as links, as the -snl switch of 7-Zip.

public static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, SevenZipCompressionOptions? options = null, bool includeBaseDirectory = false)

Parameters

sourceDirectoryName string
The directory to archive.
destinationArchiveFileName string
The path of the archive to create.
options SevenZipCompressionOptions?
The options to write the archive.
includeBaseDirectory bool
true to include the name of the directory at the root of the archive.

Example

var options = new SevenZipCompressionOptions
{
    Method = SevenZipCompressionMethod.Lzma2,
    Level = 9,
};

SevenZipFile.CreateFromDirectory(sourceDirectory, archivePath, options);

using var archive = SevenZipFile.OpenRead(archivePath);
Assert.Equal("file.txt", archive.Entries[0].FullName);

CreateFromDirectoryAsync(string, string, SevenZipCompressionOptions?, bool, CancellationToken)

SevenZipFile.cs

Creates an archive from the files and the directories of a directory. The symbolic links are stored as links, as the -snl switch of 7-Zip.

net10.0

public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, SevenZipCompressionOptions? options = null, bool includeBaseDirectory = false, CancellationToken cancellationToken = default)

net11.0

public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, SevenZipCompressionOptions? options = null, bool includeBaseDirectory = false, CancellationToken cancellationToken = default)

Parameters

sourceDirectoryName string
The directory to archive.
destinationArchiveFileName string
The path of the archive to create.
options SevenZipCompressionOptions?
The options to write the archive.
includeBaseDirectory bool
true to include the name of the directory at the root of the archive.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task A task that completes when the archive is written.

ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)

SevenZipFile.cs

Extracts an 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, .001, 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 to read the archive, such as its password.

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

SevenZipFile.cs

Extracts an 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, .001, 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 to read the archive, such as its password.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task A task that completes when the archive is extracted.

Open(string, SevenZipArchiveMode, ArchiveReaderOptions?, SevenZipCompressionOptions?)

SevenZipFile.cs

Opens an archive in a mode. A path that ends with .001 opens a multi-volume archive in SevenZipArchiveMode.Read mode.

public static SevenZipArchive Open(string archiveFileName, SevenZipArchiveMode mode, ArchiveReaderOptions? readerOptions = null, SevenZipCompressionOptions? compressionOptions = null)

Parameters

archiveFileName string
The path of the archive.
mode SevenZipArchiveMode
The mode: SevenZipArchiveMode.Read opens an existing file, SevenZipArchiveMode.Create creates or replaces it, and SevenZipArchiveMode.Update opens it, or creates it when it does not exist.
readerOptions ArchiveReaderOptions?
The options to read the archive, such as its password.
compressionOptions SevenZipCompressionOptions?
The options to write the archive.

Returns

SevenZipArchive The archive.

OpenRead(string, ArchiveReaderOptions?)

SevenZipFile.cs

Opens an archive to read it. A path that ends with .001 opens a multi-volume archive: the volumes .002, .003, ... are found next to it, unless ArchiveReaderOptions.VolumeResolver is set.

public static SevenZipArchive OpenRead(string archiveFileName, ArchiveReaderOptions? options = null)

Parameters

archiveFileName string
The path of the archive.
options ArchiveReaderOptions?
The options to read the archive, such as its password.

Returns

SevenZipArchive The archive.