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 : ArchiveRemarks
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
ArchiveZipArchive
Constructors
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the ZipArchive class that reads an archive.
public ZipArchive(Stream stream)Parameters
streamStream- 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.csInitializes a new instance of the ZipArchive class.
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen = false)Parameters
streamStream- The archive: readable and seekable to read it, writable to create it, and readable, writable and seekable to update it.
modeZipArchiveMode- The mode of the archive.
leaveOpenbooltrueto leavestreamopen 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.csInitializes a new instance of the ZipArchive class.
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, ArchiveReaderOptions? readerOptions, ZipCompressionOptions? compressionOptions)Parameters
streamStream- 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
.z01file of an Info-ZIP split archive, the.zip.001file of 7-Zip), andArchiveReaderOptions.VolumeResolverreturns the next ones (the.zipfile is the last volume of an Info-ZIP split archive). modeZipArchiveMode- The mode of the archive.
leaveOpenbooltrueto leavestreamopen when the archive is disposed.readerOptionsArchiveReaderOptions?- The options to read the archive, or
nullfor the default options. compressionOptionsZipCompressionOptions?- The options of the new entries, or
nullfor 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
| Name | Description |
|---|---|
| Comment | 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. |
| Entries | Gets the entries of the archive, in the order of the central directory (and of their creation for the new entries). |
| Format | Gets the format of the archive. |
| Mode | Gets the mode of the archive. |
Comment
ZipArchive.csGets 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
Exceptions
NotSupportedException- The archive is in
ZipArchiveMode.Readmode. ArgumentException- The comment is longer than 65535 bytes.
Entries
ZipArchive.csGets 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; }Property value
Format
ZipArchive.csMode
ZipArchive.csMethods
| Name | Description |
|---|---|
| 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.csCreates 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
entryNamestring- The path of the entry in the archive, with
/separators. optionsZipEntryOptions?- The options of the entry, which override the
ZipCompressionOptionsof the archive.
Returns
ZipArchiveEntry The new entry.
Exceptions
NotSupportedException- The archive is in
ZipArchiveMode.Readmode, 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.csCreates 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
sourceFileNamestring- The path of the file.
entryNamestring- The path of the entry in the archive, with
/separators. optionsZipEntryOptions?- The options of the entry.
Returns
ZipArchiveEntry The new entry, written.
CreateEntryFromFileAsync(string, string, ZipEntryOptions?, CancellationToken)
ZipArchive.csCreates 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
sourceFileNamestring- The path of the file.
entryNamestring- The path of the entry in the archive, with
/separators. optionsZipEntryOptions?- The options of the entry.
cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<ZipArchiveEntry> The new entry, written.
CreateSymbolicLinkEntry(string, string, ZipEntryOptions?)
ZipArchive.csCreates 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
entryNamestring- The path of the entry in the archive, with
/separators. targetstring- The target of the link.
optionsZipEntryOptions?- The options of the entry. The compression method is ignored.
Returns
ZipArchiveEntry The new entry.
GetEntry(string)
ZipArchive.csGets the entry whose full name is entryName (an ordinal comparison), or null when there is none.
public new ZipArchiveEntry? GetEntry(string entryName)Parameters
entryNamestring- The path of the entry in the archive, with
/separators.
Returns
ZipArchiveEntry? The first entry whose ArchiveEntry.FullName is entryName, or null.