ZipFile Class
Opens, creates and extracts zip archives from paths, as ZipFile.
public static class ZipFileMethods
| Name | Description |
|---|---|
| CreateFromDirectory(string, string, ZipCompressionOptions?, bool) | Creates an archive from the files and the directories of a directory: their content, their last write time and, on Unix, their permissions (on Windows, their attributes). The symbolic links are stored as links, as Info-ZIP does with its -y option. |
| CreateFromDirectoryAsync(string, string, ZipCompressionOptions?, bool, CancellationToken) | Creates an archive from the files and the directories of a directory: their content, their last write time and, on Unix, their permissions (on Windows, their attributes). The symbolic links are stored as links, as Info-ZIP does with its -y option. |
| 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, ZipArchiveMode, ArchiveReaderOptions?, ZipCompressionOptions?) | Opens an archive in a mode. |
| OpenRead(string, ArchiveReaderOptions?) | Opens an archive to read it. The volumes of a split archive are found next to it: the .z01, .z02, ... files of an Info-ZIP split archive whose last volume is archiveFileName (or whose path is one of the volumes), or the .002, .003, ... parts that follow a .zip.001 part of 7-Zip. When ArchiveReaderOptions.VolumeResolver is set, it is used instead, and archiveFileName is the first volume. |
CreateFromDirectory(string, string, ZipCompressionOptions?, bool)
ZipFile.csCreates an archive from the files and the directories of a directory: their content, their last write time and, on Unix, their permissions (on Windows, their attributes). The symbolic links are stored as links, as Info-ZIP does with its -y option.
public static void CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName, ZipCompressionOptions? options = null, bool includeBaseDirectory = false)Parameters
sourceDirectoryNamestring- The directory to archive.
destinationArchiveFileNamestring- The path of the archive to create.
optionsZipCompressionOptions?- The options to write the archive.
includeBaseDirectorybooltrueto include the name of the directory at the root of the archive.
Example
var options = new ZipCompressionOptions
{
CompressionMethod = ZipCompressionMethod.Xz,
CompressionLevel = CompressionLevel.SmallestSize,
};
ZipFile.CreateFromDirectory(sourceDirectory, archivePath, options);
using var archive = ZipFile.OpenRead(archivePath);
Assert.Equal("file.txt", archive.Entries[0].FullName);CreateFromDirectoryAsync(string, string, ZipCompressionOptions?, bool, CancellationToken)
ZipFile.csCreates an archive from the files and the directories of a directory: their content, their last write time and, on Unix, their permissions (on Windows, their attributes). The symbolic links are stored as links, as Info-ZIP does with its -y option.
net10.0
public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, ZipCompressionOptions? options = null, bool includeBaseDirectory = false, CancellationToken cancellationToken = default)net11.0
public static Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, ZipCompressionOptions? options = null, bool includeBaseDirectory = false, CancellationToken cancellationToken = default)Parameters
sourceDirectoryNamestring- The directory to archive.
destinationArchiveFileNamestring- The path of the archive to create.
optionsZipCompressionOptions?- 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?)
ZipFile.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 last volume, or the
.zip.001part, of a split 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)
ZipFile.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 last volume, or the
.zip.001part, of a split 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, ZipArchiveMode, ArchiveReaderOptions?, ZipCompressionOptions?)
ZipFile.csOpens an archive in a mode.
public static ZipArchive Open(string archiveFileName, ZipArchiveMode mode, ArchiveReaderOptions? readerOptions = null, ZipCompressionOptions? compressionOptions = null)Parameters
archiveFileNamestring- The path of the archive.
modeZipArchiveMode- The mode:
ZipArchiveMode.Readopens an existing file (asZipFile.OpenRead),ZipArchiveMode.Createcreates or replaces it, andZipArchiveMode.Updateopens it, or creates it when it does not exist. readerOptionsArchiveReaderOptions?- The options to read the archive, such as its password.
compressionOptionsZipCompressionOptions?- The options of the new entries.
Returns
ZipArchive The archive.
OpenRead(string, ArchiveReaderOptions?)
ZipFile.csOpens an archive to read it. The volumes of a split archive are found next to it: the .z01, .z02, ... files of an Info-ZIP split archive whose last volume is archiveFileName (or whose path is one of the volumes), or the .002, .003, ... parts that follow a .zip.001 part of 7-Zip. When ArchiveReaderOptions.VolumeResolver is set, it is used instead, and archiveFileName is the first volume.
public static ZipArchive 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
ZipArchive The archive.
Exceptions
InvalidDataException- The file is not a zip archive, or the archive is corrupted.