SevenZipFile Class
Opens, creates and extracts 7z archives from paths, as ZipFile.
public static class SevenZipFileMethods
| Name | Description |
|---|---|
| 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.csCreates 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
sourceDirectoryNamestring- The directory to archive.
destinationArchiveFileNamestring- The path of the archive to create.
optionsSevenZipCompressionOptions?- The options to write the archive.
includeBaseDirectorybooltrueto 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.csCreates 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
sourceDirectoryNamestring- The directory to archive.
destinationArchiveFileNamestring- The path of the archive to create.
optionsSevenZipCompressionOptions?- The options to write the archive.
includeBaseDirectorybooltrueto include the name of the directory at the root of the archive.cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that completes when the archive is written.
ExtractToDirectory(string, string, bool, ArchiveReaderOptions?)
SevenZipFile.csExtracts 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
sourceArchiveFileNamestring- The path of the archive (the first volume,
.001, of a multi-volume archive). 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, such as its password.
ExtractToDirectoryAsync(string, string, bool, ArchiveReaderOptions?, CancellationToken)
SevenZipFile.csExtracts 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
sourceArchiveFileNamestring- The path of the archive (the first volume,
.001, of a multi-volume archive). 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, such as its password.
cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that completes when the archive is extracted.
Open(string, SevenZipArchiveMode, ArchiveReaderOptions?, SevenZipCompressionOptions?)
SevenZipFile.csOpens 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
archiveFileNamestring- The path of the archive.
modeSevenZipArchiveMode- The mode:
SevenZipArchiveMode.Readopens an existing file,SevenZipArchiveMode.Createcreates or replaces it, andSevenZipArchiveMode.Updateopens it, or creates it when it does not exist. readerOptionsArchiveReaderOptions?- The options to read the archive, such as its password.
compressionOptionsSevenZipCompressionOptions?- The options to write the archive.
Returns
SevenZipArchive The archive.
OpenRead(string, ArchiveReaderOptions?)
SevenZipFile.csOpens 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
archiveFileNamestring- The path of the archive.
optionsArchiveReaderOptions?- The options to read the archive, such as its password.
Returns
SevenZipArchive The archive.