Edit

Share via


ZipFile.Open Method

Definition

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

Overloads

Open(String, ZipArchiveMode, Encoding)

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

Open(String, ZipArchiveMode)

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

Open(String, ZipArchiveMode, Encoding)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

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

public:
 static System::IO::Compression::ZipArchive ^ Open(System::String ^ archiveFileName, System::IO::Compression::ZipArchiveMode mode, System::Text::Encoding ^ entryNameEncoding);
public static System.IO.Compression.ZipArchive Open (string archiveFileName, System.IO.Compression.ZipArchiveMode mode, System.Text.Encoding entryNameEncoding);
public static System.IO.Compression.ZipArchive Open (string archiveFileName, System.IO.Compression.ZipArchiveMode mode, System.Text.Encoding? entryNameEncoding);
static member Open : string * System.IO.Compression.ZipArchiveMode * System.Text.Encoding -> System.IO.Compression.ZipArchive
Public Shared Function Open (archiveFileName As String, mode As ZipArchiveMode, entryNameEncoding As Encoding) As ZipArchive

Parameters

archiveFileName
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
ZipArchiveMode

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

entryNameEncoding
Encoding

The encoding to use when reading or writing entry names and comments 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 or comments.

Returns

The opened zip archive.

Exceptions

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.

archiveFileName is null.

In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

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

archiveFileName could not be opened.

-or-

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

-or-

An unspecified I/O error occurred while opening the file.

archiveFileName specifies a directory.

-or-

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

mode specifies an invalid value.

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

archiveFileName contains an invalid format.

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 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 and comments 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 and comment.

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

When you open a zip archive file for reading and entryNameEncoding is set to a value other than null, entry names and comments 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 and comment.

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

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

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

  • For entry names or comments that contain only ASCII characters, the language encoding flag is not set, and entry names and comments 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 and comments 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.

Applies to

Open(String, ZipArchiveMode)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

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

public:
 static System::IO::Compression::ZipArchive ^ Open(System::String ^ archiveFileName, System::IO::Compression::ZipArchiveMode mode);
public static System.IO.Compression.ZipArchive Open (string archiveFileName, System.IO.Compression.ZipArchiveMode mode);
static member Open : string * System.IO.Compression.ZipArchiveMode -> System.IO.Compression.ZipArchive
Public Shared Function Open (archiveFileName As String, mode As ZipArchiveMode) As ZipArchive

Parameters

archiveFileName
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
ZipArchiveMode

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

Returns

The opened zip archive.

Exceptions

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

archiveFileName is null.

In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

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

archiveFileName could not be opened.

-or-

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

-or-

An unspecified I/O error occurred while opening the file.

archiveFileName specifies a directory.

-or-

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

mode specifies an invalid value.

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

archiveFileName contains an invalid format.

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.

Examples

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

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}
open System.IO.Compression

let zipPath = @"c:\users\exampleuser\start.zip"
let extractPath = @"c:\users\exampleuser\extract"
let newFile = @"c:\users\exampleuser\NewFile.txt"

do
    use archive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
    archive.CreateEntryFromFile(newFile, "NewEntry.txt") |> ignore
    archive.ExtractToDirectory extractPath
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

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.

Applies to