ZipFile.Open 方法

定义

以指定的模式打开指定路径上的 zip 存档。Opens a zip archive at the specified path and in the specified mode.

重载

Open(String, ZipArchiveMode)

以指定的模式打开指定路径上的 zip 存档。Opens a zip archive at the specified path and in the specified mode.

Open(String, ZipArchiveMode, Encoding)

在指定模式下,在指定路径中,使用项名称的指定字符编码打开 zip 存档。Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.

Open(String, ZipArchiveMode)

以指定的模式打开指定路径上的 zip 存档。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

参数

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.

返回

打开的 zip 存档。The opened zip archive.

异常

archiveFileNameEmpty,仅包含空格,或者包含至少一个无效字符。archiveFileName is Empty, contains only white space, or contains at least one invalid character.

archiveFileNamenullarchiveFileName is null.

archiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

archiveFileName 无效或不存在(例如,在未映射的驱动器上)。archiveFileName is invalid or does not exist (for example, it is on an unmapped drive).

未能打开 archiveFileNamearchiveFileName could not be opened.

- 或 --or- mode 设置为 Create,但在 archiveFileName 中指定的文件已存在。mode is set to Create, but the file specified in archiveFileName already exists.

archiveFileName 指定目录。archiveFileName specifies a directory.

- 或 --or- 调用方没有访问在 archiveFileName 中指定的文件的所需权限。The caller does not have the required permission to access the file specified in archiveFileName.

mode 指定了一个无效值。mode specifies an invalid value.

mode 设置为 Read,但在 archiveFileName 中指定的文件未找到。mode is set to Read, but the file specified in archiveFileName is not found.

archiveFileName 包含无效的格式。archiveFileName contains an invalid format.

archiveFileName 无法解释为 zip 存档文件。archiveFileName could not be interpreted as a zip archive.

- 或 --or- modeUpdate,但项已丢失或已损坏,因此无法读取。mode is Update, but an entry is missing or corrupt and cannot be read.

- 或 --or- modeUpdate,但项太大,无法装入内存。mode is Update, but an entry is too large to fit into memory.

示例

下面的示例演示如何在更新模式下打开 zip 存档并将条目添加到存档。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);
            } 
        }
    }
}
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

注解

mode 参数设置为 Read时,会以文件模式值的形式从 FileMode 枚举中 Open 打开存档。When you set the mode parameter to Read, the archive is opened with Open from the FileMode enumeration as the file mode value. 如果存档不存在,则会引发 FileNotFoundException 异常。If the archive does not exist, a FileNotFoundException exception is thrown. mode 参数设置为 Read 等效于调用 OpenRead 方法。Setting the mode parameter to Read is equivalent to calling the OpenRead method.

mode 参数设置为 Create时,会以 FileMode.CreateNew 文件模式值打开存档。When you set the mode parameter to Create, the archive is opened with FileMode.CreateNew as the file mode value. 如果存档已存在,则会引发 IOExceptionIf the archive already exists, an IOException is thrown.

mode 参数设置为 Update时,会以 FileMode.OpenOrCreate 文件模式值打开存档。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. 如果存档不存在,则创建新的存档;但是,在 Update 模式下创建 zip 存档并不像在 Create 模式下创建它那样高效。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.

Open(String, ZipArchiveMode, Encoding)

在指定模式下,在指定路径中,使用项名称的指定字符编码打开 zip 存档。Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.

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);
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

参数

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 in this archive. 仅当需要针对具有不支持项名的 UTF-8 编码的 zip 归档工具和库的互操作性进行编码时,为此参数指定一个值。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.

返回

打开的 zip 存档。The opened zip archive.

异常

archiveFileNameEmpty,仅包含空格,或者包含至少一个无效字符。archiveFileName is Empty, contains only white space, or contains at least one invalid character.

- 或 --or- entryNameEncoding 设置为不同于 UTF-8 的 Unicode 编码。entryNameEncoding is set to a Unicode encoding other than UTF-8.

archiveFileNamenullarchiveFileName is null.

archiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。In archiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

archiveFileName 无效或不存在(例如,在未映射的驱动器上)。archiveFileName is invalid or does not exist (for example, it is on an unmapped drive).

未能打开 archiveFileNamearchiveFileName could not be opened.

- 或 --or- mode 设置为 Create,但在 archiveFileName 中指定的文件已存在。mode is set to Create, but the file specified in archiveFileName already exists.

archiveFileName 指定目录。archiveFileName specifies a directory.

- 或 --or- 调用方没有访问在 archiveFileName 中指定的文件的所需权限。The caller does not have the required permission to access the file specified in archiveFileName.

mode 指定了一个无效值。mode specifies an invalid value.

mode 设置为 Read,但在 archiveFileName 中指定的文件未找到。mode is set to Read, but the file specified in archiveFileName is not found.

archiveFileName 包含无效的格式。archiveFileName contains an invalid format.

archiveFileName 无法解释为 zip 存档文件。archiveFileName could not be interpreted as a zip archive.

- 或 --or- modeUpdate,但项已丢失或已损坏,因此无法读取。mode is Update, but an entry is missing or corrupt and cannot be read.

- 或 --or- modeUpdate,但项太大,无法装入内存。mode is Update, but an entry is too large to fit into memory.

注解

mode 参数设置为 Read时,会以 FileMode.Open 文件模式值打开存档。When you set the mode parameter to Read, the archive is opened with FileMode.Open as the file mode value. 如果存档不存在,则会引发 FileNotFoundException 异常。If the archive does not exist, a FileNotFoundException exception is thrown. mode 参数设置为 Read 等效于调用 OpenRead 方法。Setting the mode parameter to Read is equivalent to calling the OpenRead method.

mode 参数设置为 Create时,会以 FileMode.CreateNew 文件模式值打开存档。When you set the mode parameter to Create, the archive is opened with FileMode.CreateNew as the file mode value. 如果存档已存在,则会引发 IOExceptionIf the archive already exists, an IOException is thrown.

mode 参数设置为 Update时,会以 FileMode.OpenOrCreate 文件模式值打开存档。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. 如果存档不存在,则创建新的存档;但是,在 Update 模式下创建 zip 存档并不像在 Create 模式下创建它那样高效。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.

打开要读取的 zip 存档文件并将 entryNameEncoding 设置为 null时,将根据以下规则对条目名称进行解码: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.

  • 如果设置了语言编码标志,则使用 UTF-8 对条目名称进行解码。When the language encoding flag is set, UTF-8 is used to decode the entry name.

打开 zip 存档文件进行读取,并将 entryNameEncoding 设置为 null以外的值时,将根据以下规则对条目名称进行解码: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:

  • 如果未设置语言编码标志,则使用指定的 entryNameEncoding 对项名称进行解码。When the language encoding flag is not set, the specified entryNameEncoding is used to decode the entry name.

  • 如果设置了语言编码标志,则使用 UTF-8 对条目名称进行解码。When the language encoding flag is set, UTF-8 is used to decode the entry name.

写入存档文件并将 entryNameEncoding 设置为 null时,将根据以下规则对条目名称进行编码:When you write to archive files and entryNameEncoding is set to null, entry names are encoded according to the following rules:

  • 对于包含 ASCII 范围外的字符的条目名称,设置语言编码标志,并使用 UTF-8 对条目名称进行编码。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.

  • 对于仅包含 ASCII 字符的条目名称,未设置语言编码标志,并且通过使用当前系统默认代码页对条目名称进行编码。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.

当你写入存档文件并且 entryNameEncoding 设置为 null以外的值时,将使用指定的 entryNameEncoding 将输入名称编码为个字节。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. 仅当指定的编码为 UTF-8 编码时,才设置语言编码标志(本地文件头的通用位标志)。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.

适用于