ZipFile.Open Method (String, ZipArchiveMode, Encoding)

.NET Framework 4.5

Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.

Namespace:  System.IO.Compression
Assembly:  System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)

public static ZipArchive Open(
	string archiveFileName,
	ZipArchiveMode mode,
	Encoding entryNameEncoding
)

Parameters

archiveFileName
Type: System.String

The path to the archive to open, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

mode
Type: System.IO.Compression.ZipArchiveMode

One of the enumeration values that specifies the actions that are allowed on the entries in the opened archive.

entryNameEncoding
Type: System.Text.Encoding

The encoding to use when reading or writing entry names in this archive. Specify a value for this parameter only when an encoding is required for interoperability with zip archive tools and libraries that do not support UTF-8 encoding for entry names.

Return Value

Type: System.IO.Compression.ZipArchive
The opened zip archive.

ExceptionCondition
ArgumentException

archiveFileName is Empty, contains only white space, or contains at least one invalid character.

-or-

entryNameEncoding is set to a Unicode encoding other than UTF-8.

ArgumentNullException

archiveFileName is null.

PathTooLongException

In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must not exceed 248 characters, and file names must not exceed 260 characters.

DirectoryNotFoundException

archiveFileName is invalid or does not exist (for example, it is on an unmapped drive).

IOException

archiveFileName could not be opened.

-or-

mode is set to Create, but the file specified in archiveFileName already exists.

UnauthorizedAccessException

archiveFileName specifies a directory.

-or-

The caller does not have the required permission to access the file specified in archiveFileName.

ArgumentOutOfRangeException

mode specifies an invalid value.

FileNotFoundException

mode is set to Read, but the file specified in archiveFileName is not found.

NotSupportedException

archiveFileName contains an invalid format.

InvalidDataException

archiveFileName could not be interpreted as a zip archive.

-or-

mode is Update, but an entry is missing or corrupt and cannot be read.

-or-

mode is Update, but an entry is too large to fit into memory.

When you set the mode parameter to Read, the archive is opened with FileMode.Open as the file mode value. If the archive does not exist, a FileNotFoundException exception is thrown. Setting the mode parameter to Read is equivalent to calling the OpenRead method.

When you set the mode parameter to Create, the archive is opened with FileMode.CreateNew as the file mode value. If the archive already exists, an IOException is thrown.

When you set the mode parameter to Update, the archive is opened with FileMode.OpenOrCreate as the file mode value. If the archive exists, it is opened. The existing entries can be modified and new entries can be created. If the archive does not exist, a new archive is created; however, creating a zip archive in Update mode is not as efficient as creating it in Create mode.

When you open a zip archive file for reading and entryNameEncoding is set to null, entry names are decoded according to the following rules:

  • When the language encoding flag (in the general-purpose bit flag of the local file header) is not set, the current system default code page is used to decode the entry name.

  • When the language encoding flag is set, UTF-8 is used to decode the entry name.

When you open a zip archive file for reading and entryNameEncoding is set to a value other than null, entry names are decoded according to the following rules:

  • When the language encoding flag is not set, the specified entryNameEncoding is used to decode the entry name.

  • When the language encoding flag is set, UTF-8 is used to decode the entry name.

When you write to archive files and entryNameEncoding is set to null, entry names are encoded according to the following rules:

  • For entry names that contain characters outside the ASCII range, the language encoding flag is set, and entry names are encoded by using UTF-8.

  • For entry names that contain only ASCII characters, the language encoding flag is not set, and entry names are encoded by using the current system default code page.

When you write to archive files and entryNameEncoding is set to a value other than null, the specified entryNameEncoding is used to encode the entry names into bytes. The language encoding flag (in the general-purpose bit flag of the local file header) is set only when the specified encoding is a UTF-8 encoding.

.NET Framework

Supported in: 4.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft