ZipFile Class

Opens, creates and extracts zip archives from paths, as ZipFile.

public static class ZipFile

Methods

NameDescription
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.cs

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.

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

Parameters

sourceDirectoryName string
The directory to archive.
destinationArchiveFileName string
The path of the archive to create.
options ZipCompressionOptions?
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 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.cs

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.

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

sourceDirectoryName string
The directory to archive.
destinationArchiveFileName string
The path of the archive to create.
options ZipCompressionOptions?
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?)

ZipFile.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 last volume, or the .zip.001 part, of a split 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)

ZipFile.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 last volume, or the .zip.001 part, of a split 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, ZipArchiveMode, ArchiveReaderOptions?, ZipCompressionOptions?)

ZipFile.cs

Opens an archive in a mode.

public static ZipArchive Open(string archiveFileName, ZipArchiveMode mode, ArchiveReaderOptions? readerOptions = null, ZipCompressionOptions? compressionOptions = null)

Parameters

archiveFileName string
The path of the archive.
mode ZipArchiveMode
The mode: ZipArchiveMode.Read opens an existing file (as ZipFile.OpenRead), ZipArchiveMode.Create creates or replaces it, and ZipArchiveMode.Update opens it, or creates it when it does not exist.
readerOptions ArchiveReaderOptions?
The options to read the archive, such as its password.
compressionOptions ZipCompressionOptions?
The options of the new entries.

Returns

ZipArchive The archive.

OpenRead(string, ArchiveReaderOptions?)

ZipFile.cs

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.

public static ZipArchive 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

ZipArchive The archive.

Exceptions

InvalidDataException
The file is not a zip archive, or the archive is corrupted.