ZipFile.Open 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
以指定的模式打开指定路径上的 zip 归档。
重载
| Open(String, ZipArchiveMode, Encoding) | 在指定的模式中用指定的项名称汉字解码打开指定路径的 zip 存档。 | 
| Open(String, ZipArchiveMode) | 以指定的模式打开指定路径上的 zip 归档。 | 
Open(String, ZipArchiveMode, Encoding)
- Source:
- ZipFile.Create.cs
- Source:
- ZipFile.Create.cs
- Source:
- ZipFile.Create.cs
在指定的模式中用指定的项名称汉字解码打开指定路径的 zip 存档。
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.ZipArchivePublic Shared Function Open (archiveFileName As String, mode As ZipArchiveMode, entryNameEncoding As Encoding) As ZipArchive参数
- archiveFileName
- String
要打开的存档的路径,指定为相对路径或绝对路径。 相对路径被解释为相对于当前工作目录。
- mode
- ZipArchiveMode
一个枚举值指定在开放的存档项的操作。
- entryNameEncoding
- Encoding
在存档中读取或写入项名时使用的编码。 仅当需要针对具有不支持条目名称的 UTF-8 编码的 zip 归档工具和库的互操作性进行编码时,为此参数指定值。
返回
打开的 zip 存档。
例外
              archiveFileName 为 null。
在 archiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。
              archiveFileName 无效或不存在(例如,在未映射的驱动器上)。
              mode 指定了一个无效值。
              mode 设置为 Read,但在 archiveFileName 中指定的文件未找到。
              archiveFileName 包含无效格式。
              archiveFileName 无法解释为 zip 存档文件。
- 或 -
              mode 是 Update,但项已丢失或已损坏,因此无法读取。
- 或 -
              mode 是 Update,但项太大,无法装入内存。
注解
将 参数设置为 modeRead时,会以 FileMode.Open 作为文件模式值打开存档。 如果存档不存在, FileNotFoundException 则会引发异常。 将 mode 参数设置为 Read 等效于调用 OpenRead 方法。
将 参数设置为 modeCreate时,会以 FileMode.CreateNew 作为文件模式值打开存档。 如果存档已存在, IOException 则会引发 。
将 参数设置为 modeUpdate时,会以 FileMode.OpenOrCreate 作为文件模式值打开存档。 如果存档存在,则会打开它。 可以修改现有条目并创建新条目。 如果存档不存在,则会创建一个新的存档;但是,在 模式下 Update 创建 zip 存档不如在 模式下 Create 创建它的效率。
打开 zip 存档文件进行读取并 entryNameEncoding 设置为 null时,将根据以下规则解码条目名称:
- 如果未设置本地文件头) 的常规用途位标志中 (语言编码标志,则使用当前系统默认代码页来解码条目名称。 
- 设置语言编码标志后,使用 UTF-8 解码条目名称。 
打开 zip 存档文件进行读取并 entryNameEncoding 设置为 以外的 null值时,将根据以下规则对条目名称进行解码:
- 如果未设置语言编码标志,则使用指定的 - entryNameEncoding来解码条目名称。
- 设置语言编码标志后,使用 UTF-8 解码条目名称。 
当写入存档文件并将其 entryNameEncoding 设置为 null时,将根据以下规则对条目名称进行编码:
- 对于包含 ASCII 范围之外的字符的条目名称,设置语言编码标志,并使用 UTF-8 对条目名称进行编码。 
- 对于仅包含 ASCII 字符的条目名称,未设置语言编码标志,并且使用当前系统默认代码页对条目名称进行编码。 
当你写入存档文件并 entryNameEncoding 设置为 以外的 null值时, entryNameEncoding 指定的 用于将条目名称编码为字节。 仅当指定的编码 (为 UTF-8 编码时,才会设置本地文件头) 的常规用途位标志中的语言编码标志。
适用于
Open(String, ZipArchiveMode)
- Source:
- ZipFile.Create.cs
- Source:
- ZipFile.Create.cs
- Source:
- ZipFile.Create.cs
以指定的模式打开指定路径上的 zip 归档。
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.ZipArchivePublic Shared Function Open (archiveFileName As String, mode As ZipArchiveMode) As ZipArchive参数
- archiveFileName
- String
要打开的存档的路径,指定为相对路径或绝对路径。 相对路径被解释为相对于当前工作目录。
- mode
- ZipArchiveMode
一个枚举值指定在开放的存档项的操作。
返回
打开的 zip 存档。
例外
              archiveFileName 为 Empty,仅包含空格,或者包含至少一个无效字符。
              archiveFileName 为 null。
在 archiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。
              archiveFileName 无效或不存在(例如,在未映射的驱动器上)。
              mode 指定了一个无效值。
              mode 设置为 Read,但在 archiveFileName 中指定的文件未找到。
              archiveFileName 包含无效格式。
              archiveFileName 无法解释为 zip 存档文件。
- 或 -
              mode 是 Update,但项已丢失或已损坏,因此无法读取。
- 或 -
              mode 是 Update,但项太大,无法装入内存。
示例
以下示例演示如何在更新模式下打开 zip 存档并将条目添加到存档。
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
注解
将 参数设置为 modeRead时,会使用 OpenFileMode 枚举中的 作为文件模式值打开存档。 如果存档不存在, FileNotFoundException 则会引发异常。 将 mode 参数设置为 Read 等效于调用 OpenRead 方法。
将 参数设置为 modeCreate时,会以 FileMode.CreateNew 作为文件模式值打开存档。 如果存档已存在, IOException 则会引发 。
将 参数设置为 modeUpdate时,会以 FileMode.OpenOrCreate 作为文件模式值打开存档。 如果存档存在,则会打开它。 可以修改现有条目并创建新条目。 如果存档不存在,则会创建一个新的存档;但是,在 模式下 Update 创建 zip 存档不如在 模式下 Create 创建它的效率。