ZipFile.Open 方法

定義

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

多載

Open(String, ZipArchiveMode, Encoding)

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

Open(String, ZipArchiveMode)

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

Open(String, ZipArchiveMode, Encoding)

來源:
ZipFile.Create.cs
來源:
ZipFile.Create.cs
來源:
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

在此封存中讀取或寫入項目名稱時要使用的編碼方式。 只有當需要編碼以與 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 封存並無效率。

當您開啟 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)

來源:
ZipFile.Create.cs
來源:
ZipFile.Create.cs
來源:
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時,封存會以 Open 列舉 FileMode 開啟做為檔案模式值。 如果封存不存在, FileNotFoundException 則會擲回例外狀況。 mode將參數設定為 Read 相當於呼叫 OpenRead 方法。

當您將 mode 參數設定為 Create時,封存會以 FileMode.CreateNew 做為檔案模式值開啟。 如果封存已經存在, IOException 則會擲回 。

當您將 mode 參數設定為 Update時,封存會以 FileMode.OpenOrCreate 做為檔案模式值開啟。 如果封存存在,則會開啟。 您可以修改現有的專案,並建立新的專案。 如果封存不存在,則會建立新的封存;不過,在模式中 Update 建立 ZIP 封存,與在模式中 Create 建立 zip 封存並無效率。

適用於