ZipFile.Open 方法

定義

在指定路徑上以指定的模式開啟 zip 封存。

多載

Open(String, ZipArchiveMode, Encoding)

以指定的模式,並將指定的字元編碼方式使用於項目名稱,開啟位於指定路徑的 zip 封存。

Open(String, ZipArchiveMode)

在指定路徑上以指定的模式開啟 zip 封存。

Open(String, ZipArchiveMode, Encoding)

以指定的模式,並將指定的字元編碼方式使用於項目名稱,開啟位於指定路徑的 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

在此封存中讀取或寫入項目名稱時要使用的編碼方式。 只有當需要編碼以與 Zip 封存工具和程式庫互通,且這類工具和程式庫不支援項目名稱使用 UTF-8 編碼時,指定此參數的值。

傳回

已開啟的 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 封存檔案以供讀取且 entryNameEncoding 設定為 null 時,專案名稱會根據下列規則解碼:

  • 如果未設定本機檔) 頭的一般用途位旗標中的語言編碼旗標 (,則會使用目前的系統預設字碼頁來解碼專案名稱。

  • 設定語言編碼旗標時,會使用 UTF-8 來解碼專案名稱。

當您開啟 zip 封存檔案以供讀取,並且 entryNameEncoding 設定為 以外的 null 值時,專案名稱會根據下列規則進行解碼:

  • 未設定語言編碼旗標時,會使用指定的 entryNameEncoding 來解碼專案名稱。

  • 設定語言編碼旗標時,會使用 UTF-8 來解碼專案名稱。

當您寫入封存檔案並 entryNameEncoding 設定為 null 時,專案名稱會根據下列規則進行編碼:

  • 針對包含 ASCII 範圍外字元的專案名稱,會設定語言編碼旗標,並使用 UTF-8 編碼專案名稱。

  • 若為僅包含 ASCII 字元的專案名稱,則不會設定語言編碼旗標,而且使用目前的系統預設字碼頁來編碼專案名稱。

當您寫入封存檔案並 entryNameEncoding 設定為 以外的 null 值時,會使用指定的 entryNameEncoding 將專案名稱編碼為位元組。 只有在指定的編碼為 UTF-8 編碼時,才會設定本機檔頭的一般用途位旗標 (語言編碼旗標) 。

適用於

Open(String, ZipArchiveMode)

在指定路徑上以指定的模式開啟 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 建立它一樣有效率。

適用於