ZipFile.Open ZipFile.Open ZipFile.Open ZipFile.Open Method

定义

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

重载

Open(String, ZipArchiveMode) Open(String, ZipArchiveMode) Open(String, ZipArchiveMode) Open(String, ZipArchiveMode)

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

Open(String, ZipArchiveMode, Encoding) Open(String, ZipArchiveMode, Encoding) Open(String, ZipArchiveMode, Encoding) 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) Open(String, ZipArchiveMode) Open(String, ZipArchiveMode) 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 String String 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 ZipArchiveMode ZipArchiveMode 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 设置为 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时, 将以OpenFileMode枚举到的文件模式值打开存档。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. 将参数设置为Read等效于调用OpenRead方法。 modeSetting 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. 如果存档已存在, IOException则会引发。If 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) Open(String, ZipArchiveMode, Encoding) Open(String, ZipArchiveMode, Encoding) 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 String String 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 ZipArchiveMode ZipArchiveMode ZipArchiveMode

一个枚举值指定在开放的存档项的操作。One of the enumeration values that specifies the actions that are allowed on the entries in the opened archive.

entryNameEncoding
Encoding Encoding Encoding 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 设置为 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. 将参数设置为Read等效于调用OpenRead方法。 modeSetting 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. 如果存档已存在, IOException则会引发。If 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.

适用于