# ZipFile.Open Method (String, ZipArchiveMode)

.NET Framework (current version)

Opens a zip archive at the specified path and in the specified mode.

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

## Syntax

Public Shared Function Open (
archiveFileName As String,
mode As ZipArchiveMode
) As ZipArchive


#### 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 which are allowed on the entries in the opened archive.

#### Return Value

Type: System.IO.Compression.ZipArchive

The opened zip archive.

## Exceptions

Exception Condition
ArgumentException

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

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.

## Remarks

When you set the mode parameter to Read, the archive is opened with Open from the FileMode enumeration 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.

## Examples

The following example shows how to open a zip archive in the update mode and add an entry to the archive.

Imports System.IO
Imports System.IO.Compression

Module Module1

Sub Main()
Dim zipPath As String = "c:\users\exampleuser\end.zip"
Dim extractPath As String = "c:\users\exampleuser\extract"
Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
archive.ExtractToDirectory(extractPath)
End Using
End Sub

End Module


## Version Information

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.5

Return to top
Show: