ZipFile.Open 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在指定路徑和模式下開啟壓縮壓縮檔。
多載
| 名稱 | Description |
|---|---|
| Open(String, ZipArchiveMode) |
在指定路徑和模式下開啟壓縮壓縮檔。 |
| Open(String, ZipArchiveMode, Encoding) |
在指定路徑、指定模式下,並使用指定的字元編碼來開啟一個 zip 壓縮檔。 |
Open(String, ZipArchiveMode)
在指定路徑和模式下開啟壓縮壓縮檔。
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
其中一個列舉值,指定對開啟檔案中條目允許的行動。
傳回
開啟的壓縮壓縮檔。
例外狀況
archiveFileName 是 Empty,僅包含空白,或至少包含一個無效字元。
archiveFileName 是 null。
在 archiveFileName中,指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。
archiveFileName 無效或不存在(例如,它位於未映射的磁碟機上)。
mode 指定一個無效值。
mode設定為 Read,但找不到該檔案。archiveFileName
archiveFileName 包含無效格式。
範例
以下範例展示了如何在更新模式下開啟壓縮壓縮檔並新增一個項目到壓縮檔。
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為 時,檔案庫會以列舉中的檔案模式值開啟ReadOpen。FileMode 若該檔案不存在, FileNotFoundException 則會拋出例外。 將參數設 mode 為 等 Read 同於呼叫該 OpenRead 方法。
當你將參數設 mode 為 Create時,檔案會以檔案模式值開啟 FileMode.CreateNew 壓縮檔。 如果該檔案已經存在,則 IOException 會拋棄。
當你將參數設 mode 為 Update時,檔案會以檔案模式值開啟 FileMode.OpenOrCreate 壓縮檔。 如果檔案存在,它就會被打開。 現有條目可以被修改,也可以建立新的條目。 若該檔案庫不存在,則會建立一個新的檔案;然而,在 Mode 中 Update 建立壓縮壓縮檔的效率不如在 Mode 中 Create 建立。
適用於
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);
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 壓縮工具和函式庫互通時,才指定此參數值。
傳回
開啟的壓縮壓縮檔。
例外狀況
archiveFileName 是 null。
在 archiveFileName中,指定的路徑、檔案名稱或兩者都超過系統定義的最大長度。
archiveFileName 無效或不存在(例如,它位於未映射的磁碟機上)。
mode 指定一個無效值。
mode設定為 Read,但找不到該檔案。archiveFileName
archiveFileName 包含無效格式。
備註
當你將參數設 mode 為 Read時,檔案會以檔案模式值開啟 FileMode.Open 壓縮檔。 若該檔案不存在, FileNotFoundException 則會拋出例外。 將參數設 mode 為 等 Read 同於呼叫該 OpenRead 方法。
當你將參數設 mode 為 Create時,檔案會以檔案模式值開啟 FileMode.CreateNew 壓縮檔。 如果該檔案已經存在,則 IOException 會拋棄。
當你將參數設 mode 為 Update時,檔案會以檔案模式值開啟 FileMode.OpenOrCreate 壓縮檔。 如果檔案存在,它就會被打開。 現有條目可以被修改,也可以建立新的條目。 若該檔案庫不存在,則會建立一個新的檔案;然而,在 Mode 中 Update 建立壓縮壓縮檔的效率不如在 Mode 中 Create 建立。
當您開啟一個設定為 的entryNameEncoding壓縮壓縮檔來讀取null時,條目名稱和註解會依照以下規則解碼:
- 當語言編碼標誌(本地檔案標頭的通用位元標誌)未設定時,系統會使用當前系統預設代碼頁來解碼條目名稱與註解。
- 當語言編碼標誌設定時,會使用 UTF-8 來解碼條目名稱與註解。
當你打開一個 zip 壓縮檔來讀取,且 entryNameEncoding 設定為 以外的 null值時,條目名稱和註解會依照以下規則解碼:
- 當語言編碼標誌未設定時,指定標記
entryNameEncoding會用來解碼條目名稱與註解。 - 當語言編碼標誌設定時,會使用 UTF-8 來解碼條目名稱與註解。
當你寫入 archive 檔案時, entryNameEncoding 且設定為 null,條目名稱和註解會依照以下規則編碼:
- 對於包含 ASCII 範圍外字元的項目名稱或註解,則設定語言編碼標誌,並以 UTF-8 編碼項目名稱與註解。
- 對於僅包含 ASCII 字元的項目名稱或註解,則不會設定語言編碼標誌,條目名稱與註解則使用目前系統預設的代碼頁來編碼。
當你寫入壓縮檔檔案時 entryNameEncoding ,設定為非 null的值,指定值 entryNameEncoding 會用來將項目名稱和註解編碼成位元組。 語言編碼旗標(在本地檔案標頭的通用位元旗標中)僅在指定的編碼為 UTF-8 編碼時設定。