ZipFile.Open 方法

定义

在指定路径和指定模式下打开 zip 存档。

重载

名称 说明
Open(String, ZipArchiveMode)

在指定路径和指定模式下打开 zip 存档。

Open(String, ZipArchiveMode, Encoding)

在指定模式下,在指定路径上打开 zip 存档,并使用指定字符编码输入名称和注释。

Open(String, ZipArchiveMode)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
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.ZipArchive
Public Shared Function Open (archiveFileName As String, mode As ZipArchiveMode) As ZipArchive

参数

archiveFileName
String

要打开的存档的路径,指定为相对路径或绝对路径。 相对路径被解释为相对于当前工作目录。

mode
ZipArchiveMode

枚举值之一,指定在打开的存档中的条目上允许的操作。

返回

打开的 zip 存档。

例外

archiveFileNameEmpty,仅包含空格,或至少包含一个无效字符。

archiveFileNamenull

在 中 archiveFileName,指定的路径、文件名或两者都超过了系统定义的最大长度。

archiveFileName 无效或不存在(例如,它位于未映射的驱动器上)。

archiveFileName 无法打开。

-或-

mode 设置为 Create,但已存在指定的 archiveFileName 文件。

-或-

打开文件时出现未指定的 I/O 错误。

archiveFileName 指定目录。

-或-

调用方没有访问在中指定的 archiveFileName文件所需的权限。

mode 指定无效值。

mode 设置为 Read,但找不到指定的 archiveFileName 文件。

archiveFileName 包含无效格式。

archiveFileName 无法解释为 zip 存档。

-或-

modeUpdate,但条目缺失或损坏,无法读取。

-或-

modeUpdate,但条目太大,无法容纳到内存中。

示例

以下示例演示如何在更新模式下打开 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

注解

将参数mode设置为 Read 时,将从枚举打开OpenFileMode存档作为文件模式值。 如果存档不存在, FileNotFoundException 则会引发异常。 将 mode 参数设置为 Read 等效于调用 OpenRead 方法。

将参数mode设置为Create时,存档会以文件模式值的形式打开FileMode.CreateNew。 如果存档已存在,则会引发一个 IOException

将参数mode设置为Update时,存档会以文件模式值的形式打开FileMode.OpenOrCreate。 如果存档存在,则会打开它。 可以修改现有条目,并且可以创建新条目。 如果存档不存在,则会创建新的存档;但是,在 Update 模式下创建 zip 存档不如在 Create 模式下创建 zip 存档那么高效。

适用于

Open(String, ZipArchiveMode, Encoding)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
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.ZipArchive
Public Shared Function Open (archiveFileName As String, mode As ZipArchiveMode, entryNameEncoding As Encoding) As ZipArchive

参数

archiveFileName
String

要打开的存档的路径,指定为相对路径或绝对路径。 相对路径被解释为相对于当前工作目录。

mode
ZipArchiveMode

枚举值之一,指定在打开的存档中的条目上允许的操作。

entryNameEncoding
Encoding

在此存档中读取或写入条目名称和注释时要使用的编码。 仅当需要编码才能与不支持条目名称或注释的 UTF-8 编码的 zip 存档工具和库的互作性时,才为此参数指定值。

返回

打开的 zip 存档。

例外

archiveFileNameEmpty,仅包含空格,或至少包含一个无效字符。

-或-

entryNameEncoding 设置为 UTF-8 以外的 Unicode 编码。

archiveFileNamenull

在 中 archiveFileName,指定的路径、文件名或两者都超过了系统定义的最大长度。

archiveFileName 无效或不存在(例如,它位于未映射的驱动器上)。

archiveFileName 无法打开。

-或-

mode 设置为 Create,但已存在指定的 archiveFileName 文件。

-或-

打开文件时出现未指定的 I/O 错误。

archiveFileName 指定目录。

-或-

调用方没有访问在中指定的 archiveFileName文件所需的权限。

mode 指定无效值。

mode 设置为 Read,但找不到指定的 archiveFileName 文件。

archiveFileName 包含无效格式。

archiveFileName 无法解释为 zip 存档。

-或-

modeUpdate,但条目缺失或损坏,无法读取。

-或-

modeUpdate,但条目太大,无法容纳到内存中。

注解

将参数mode设置为Read时,存档会以文件模式值的形式打开FileMode.Open。 如果存档不存在, FileNotFoundException 则会引发异常。 将 mode 参数设置为 Read 等效于调用 OpenRead 方法。

将参数mode设置为Create时,存档会以文件模式值的形式打开FileMode.CreateNew。 如果存档已存在,则会引发一个 IOException

将参数mode设置为Update时,存档会以文件模式值的形式打开FileMode.OpenOrCreate。 如果存档存在,则会打开它。 可以修改现有条目,并且可以创建新条目。 如果存档不存在,则会创建新的存档;但是,在 Update 模式下创建 zip 存档不如在 Create 模式下创建 zip 存档那么高效。

打开 zip 存档文件进行读取并 entryNameEncoding 设置为 null以下规则时,条目名称和注释将按以下规则进行解码:

  • 如果未设置语言编码标志(在本地文件头的常规用途位标志中),则当前系统默认代码页用于解码条目名称和注释。
  • 设置语言编码标志时,UTF-8 用于解码条目名称和注释。

打开 zip 存档文件进行读取并 entryNameEncoding 设置为非 null值时,条目名称和注释将按照以下规则进行解码:

  • 如果未设置语言编码标志,则指定 entryNameEncoding 用于解码条目名称和注释。
  • 设置语言编码标志时,UTF-8 用于解码条目名称和注释。

写入存档文件并将其 entryNameEncoding 设置为 null时,条目名称和注释将按照以下规则进行编码:

  • 对于包含 ASCII 范围之外的字符的条目名称或注释,将设置语言编码标志,并使用 UTF-8 对条目名称和注释进行编码。
  • 对于仅包含 ASCII 字符的条目名称或注释,未设置语言编码标志,并且使用当前系统默认代码页对条目名称和注释进行编码。

写入存档文件并将其 entryNameEncoding 设置为其他 null值时,指定的 entryNameEncoding 值用于将条目名称和注释编码为字节。 仅当指定的编码为 UTF-8 编码时,才会设置语言编码标志(在本地文件头的常规用途位标志中)。

适用于