ZipArchive Class

A zip archive, read, created or updated as ZipArchive, with the compression methods and the encryptions that it does not support. In ZipArchiveMode.Read mode, the entries are read from the central directory when the archive is opened, and their content can be read in any order.

public sealed class ZipArchive : Archive

Remarks

Reading supports the compression methods of ZipCompressionMethod that are not marked as not supported, the traditional PKWARE encryption (ZipCrypto) and the WinZip AES encryption, zip64 archives, archives with data prepended (self-extracting archives), and split archives: the Info-ZIP .z01, .z02, ..., .zip volumes and the .zip.001, .zip.002, ... parts of 7-Zip.

Writing supports the Stored, Deflate, BZip2, LZMA, PPMd, XZ and Zstandard (on .NET 11) methods, the WinZip AES (128, 192 and 256 bits) and ZipCrypto encryptions, and zip64. In ZipArchiveMode.Create mode, the stream does not need to be seekable: each entry is written when its content is written, one entry at a time. On a seekable stream, the local header of an entry is rewritten with its sizes once its content is written; otherwise, a data descriptor follows the content. The central directory is written when the archive is disposed. In ZipArchiveMode.Update mode, the archive is rewritten when it is disposed, if it is modified: the existing entries are copied without being decoded (their password is not needed), and the new entries are written to a temporary file until then.

The names that are not marked as UTF-8 (general purpose flag bit 11) nor have an Info-ZIP Unicode path extra field are decoded with ArchiveReaderOptions.EntryNameEncoding, or with the code page 437 when it is null. The backslashes of the names of the entries created on MS-DOS or Windows are replaced with /. The names and the comments written are ASCII, or UTF-8 with the flag bit 11. A password is encoded in UTF-8.

Example

using var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
    // One entry is written at a time; the stream does not need to be seekable
    var entry = archive.CreateEntry("docs/readme.txt", new ZipEntryOptions { CompressionMethod = ZipCompressionMethod.Lzma });
    using var writer = new StreamWriter(entry.Open());
    writer.Write("Hello from zip");
}

stream.Position = 0;
using (var archive = new ZipArchive(stream))
{
    var entry = archive.GetEntry("docs/readme.txt")!;
    Assert.Equal(ZipCompressionMethod.Lzma, entry.CompressionMethod);
    using var reader = new StreamReader(entry.Open());
    Assert.Equal("Hello from zip", reader.ReadToEnd());
}
Inheritance
Archive ZipArchive

Constructors

NameDescription
ZipArchive(Stream)Initializes a new instance of the ZipArchive class that reads an archive.
ZipArchive(Stream, ZipArchiveMode, bool)Initializes a new instance of the ZipArchive class.
ZipArchive(Stream, ZipArchiveMode, bool, ArchiveReaderOptions?, ZipCompressionOptions?)Initializes a new instance of the ZipArchive class.

ZipArchive overloads

ZipArchive(Stream)

ZipArchive.cs

Initializes a new instance of the ZipArchive class that reads an archive.

public ZipArchive(Stream stream)
Parameters
stream Stream
The archive. It must be readable and seekable.
Exceptions
ArgumentException
The stream is not readable or not seekable.
InvalidDataException
The stream is not a zip archive, or the archive is corrupted.
NotSupportedException
The central directory of the archive is encrypted.

ZipArchive(Stream, ZipArchiveMode, bool)

ZipArchive.cs

Initializes a new instance of the ZipArchive class.

public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen = false)
Parameters
stream Stream
The archive: readable and seekable to read it, writable to create it, and readable, writable and seekable to update it.
mode ZipArchiveMode
The mode of the archive.
leaveOpen bool
true to leave stream open when the archive is disposed.
Exceptions
ArgumentException
The stream does not support the operations of the mode.
InvalidDataException
The stream is not a zip archive, or the archive is corrupted.
NotSupportedException
The central directory of the archive is encrypted.

ZipArchive(Stream, ZipArchiveMode, bool, ArchiveReaderOptions?, ZipCompressionOptions?)

ZipArchive.cs

Initializes a new instance of the ZipArchive class.

public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, ArchiveReaderOptions? readerOptions, ZipCompressionOptions? compressionOptions)
Parameters
stream Stream
The archive: readable and seekable to read it, writable to create it, and readable, writable and seekable to update it. To read a split archive, it is the first volume (the .z01 file of an Info-ZIP split archive, the .zip.001 file of 7-Zip), and ArchiveReaderOptions.VolumeResolver returns the next ones (the .zip file is the last volume of an Info-ZIP split archive).
mode ZipArchiveMode
The mode of the archive.
leaveOpen bool
true to leave stream open when the archive is disposed.
readerOptions ArchiveReaderOptions?
The options to read the archive, or null for the default options.
compressionOptions ZipCompressionOptions?
The options of the new entries, or null for the default options.
Exceptions
ArgumentException
The stream does not support the operations of the mode.
InvalidDataException
The stream is not a zip archive, or the archive is corrupted.
NotSupportedException
The central directory of the archive is encrypted.

Properties

NameDescription
CommentGets or sets the comment of the archive. It can be set in ZipArchiveMode.Create and ZipArchiveMode.Update modes, until the archive is disposed. The comment of the end of central directory record has no encoding flag: it is written in the code page 437, the encoding the readers assume, or in UTF-8 when it has a character that code page 437 does not have.
EntriesGets the entries of the archive, in the order of the central directory (and of their creation for the new entries).
FormatGets the format of the archive.
ModeGets the mode of the archive.

Gets or sets the comment of the archive. It can be set in ZipArchiveMode.Create and ZipArchiveMode.Update modes, until the archive is disposed. The comment of the end of central directory record has no encoding flag: it is written in the code page 437, the encoding the readers assume, or in UTF-8 when it has a character that code page 437 does not have.

public string? Comment { get; set; }

Property value

string?

Exceptions

NotSupportedException
The archive is in ZipArchiveMode.Read mode.
ArgumentException
The comment is longer than 65535 bytes.

Gets the entries of the archive, in the order of the central directory (and of their creation for the new entries).

public new IReadOnlyList<ZipArchiveEntry> Entries { get; }

Gets the format of the archive.

public override ArchiveFormat Format { get; }

Property value

ArchiveFormat

Gets the mode of the archive.

public ZipArchiveMode Mode { get; }

Property value

ZipArchiveMode

Methods

NameDescription
CreateEntry(string, ZipEntryOptions?)Creates an entry. A name that ends with / creates a directory, written at once. Write the content of a file with ZipArchiveEntry.Open; an entry that is not opened is an empty file. The options of the entry are resolved when it is created: changing options afterwards has no effect.
CreateEntryFromFile(string, string, ZipEntryOptions?)Creates an entry from a file: its content, its last write time and, on Unix, its permissions (on Windows, its attributes). A symbolic link is stored as a link (its target). The options override the metadata of the file.
CreateEntryFromFileAsync(string, string, ZipEntryOptions?, CancellationToken)Creates an entry from a file: its content, its last write time and, on Unix, its permissions (on Windows, its attributes). A symbolic link is stored as a link (its target). The options override the metadata of the file.
CreateSymbolicLinkEntry(string, string, ZipEntryOptions?)Creates an entry that is a symbolic link: its Unix mode is a link (S_IFLNK, rwxrwxrwx unless ZipEntryOptions.UnixFileMode is set), and its content, stored, is the target in UTF-8, as Info-ZIP writes it.
GetEntry(string)Gets the entry whose full name is entryName (an ordinal comparison), or null when there is none.

CreateEntry(string, ZipEntryOptions?)

ZipArchive.cs

Creates an entry. A name that ends with / creates a directory, written at once. Write the content of a file with ZipArchiveEntry.Open; an entry that is not opened is an empty file. The options of the entry are resolved when it is created: changing options afterwards has no effect.

public ZipArchiveEntry CreateEntry(string entryName, ZipEntryOptions? options = null)

Parameters

entryName string
The path of the entry in the archive, with / separators.
options ZipEntryOptions?
The options of the entry, which override the ZipCompressionOptions of the archive.

Returns

ZipArchiveEntry The new entry.

Exceptions

NotSupportedException
The archive is in ZipArchiveMode.Read mode, or the compression method cannot be written (Deflate64, the legacy methods, and Zstandard before .NET 11).
InvalidOperationException
The content of another entry is being written.

CreateEntryFromFile(string, string, ZipEntryOptions?)

ZipArchive.cs

Creates an entry from a file: its content, its last write time and, on Unix, its permissions (on Windows, its attributes). A symbolic link is stored as a link (its target). The options override the metadata of the file.

public ZipArchiveEntry CreateEntryFromFile(string sourceFileName, string entryName, ZipEntryOptions? options = null)

Parameters

sourceFileName string
The path of the file.
entryName string
The path of the entry in the archive, with / separators.
options ZipEntryOptions?
The options of the entry.

Returns

ZipArchiveEntry The new entry, written.

CreateEntryFromFileAsync(string, string, ZipEntryOptions?, CancellationToken)

ZipArchive.cs

Creates an entry from a file: its content, its last write time and, on Unix, its permissions (on Windows, its attributes). A symbolic link is stored as a link (its target). The options override the metadata of the file.

net10.0

public Task<ZipArchiveEntry> CreateEntryFromFileAsync(string sourceFileName, string entryName, ZipEntryOptions? options = null, CancellationToken cancellationToken = default)

net11.0

public Task<ZipArchiveEntry> CreateEntryFromFileAsync(string sourceFileName, string entryName, ZipEntryOptions? options = null, CancellationToken cancellationToken = default)

Parameters

sourceFileName string
The path of the file.
entryName string
The path of the entry in the archive, with / separators.
options ZipEntryOptions?
The options of the entry.
cancellationToken CancellationToken
The token to monitor for cancellation requests.

Returns

Task<ZipArchiveEntry> The new entry, written.

CreateSymbolicLinkEntry(string, string, ZipEntryOptions?)

ZipArchive.cs

Creates an entry that is a symbolic link: its Unix mode is a link (S_IFLNK, rwxrwxrwx unless ZipEntryOptions.UnixFileMode is set), and its content, stored, is the target in UTF-8, as Info-ZIP writes it.

public ZipArchiveEntry CreateSymbolicLinkEntry(string entryName, string target, ZipEntryOptions? options = null)

Parameters

entryName string
The path of the entry in the archive, with / separators.
target string
The target of the link.
options ZipEntryOptions?
The options of the entry. The compression method is ignored.

Returns

ZipArchiveEntry The new entry.

GetEntry(string)

ZipArchive.cs

Gets the entry whose full name is entryName (an ordinal comparison), or null when there is none.

public new ZipArchiveEntry? GetEntry(string entryName)

Parameters

entryName string
The path of the entry in the archive, with / separators.

Returns

ZipArchiveEntry? The first entry whose ArchiveEntry.FullName is entryName, or null.