ZipArchiveMode 列舉

定義

指定與 ZIP 封存項目互動的值。Specifies values for interacting with zip archive entries.

public enum class ZipArchiveMode
public enum ZipArchiveMode
type ZipArchiveMode = 
Public Enum ZipArchiveMode
繼承
ZipArchiveMode

欄位

Create 1

僅允許建立新的封存項目。Only creating new archive entries is permitted.

Read 0

僅允許讀取封存項目。Only reading archive entries is permitted.

Update 2

封存項目允許讀取及寫入作業。Both read and write operations are permitted for archive entries.

範例

下列範例顯示如何在建立 ZipArchive 物件時,指定 ZipArchiveMode 值。The following example shows how to specify a ZipArchiveMode value when creating a ZipArchive object.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Using zipToOpen As FileStream = New FileStream("c:\users\exampleuser\release.zip", FileMode.Open)
            Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Readme.txt")
                Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
                    writer.WriteLine("Information about this package.")
                    writer.WriteLine("========================")
                End Using
            End Using
        End Using
    End Sub

End Module

備註

當您將模式設定為 [讀取] 時,基礎檔案或資料流程必須支援讀取,但不需要支援搜尋。When you set the mode to Read, the underlying file or stream must support reading, but does not have to support seeking. 如果基礎檔案或資料流程支援搜尋,檔案就會在要求時從封存中讀取。If the underlying file or stream supports seeking, the files are read from the archive as they are requested. 如果基礎檔案或資料流程不支援搜尋,則會將整個封存保留在記憶體中。If the underlying file or stream does not support seeking, the entire archive is held in memory.

當您將模式設定為 [建立] 時,基礎檔案或資料流程必須支援寫入,但不需要支援搜尋。When you set the mode to Create, the underlying file or stream must support writing, but does not have to support seeking. 封存中的每個專案只能開啟一次進行寫入。Each entry in the archive can be opened only once for writing. 如果您建立單一專案,當資料可供使用時,就會立即寫入基礎資料流程或檔案。If you create a single entry, the data is written to the underlying stream or file as soon as it is available. 如果您建立多個專案(例如藉由呼叫 CreateFromDirectory 方法),則會在建立所有專案之後,將資料寫入基礎資料流程或檔案。If you create multiple entries, such as by calling the CreateFromDirectory method, the data is written to the underlying stream or file after all the entries are created.

當您將模式設定為 [更新] 時,基礎檔案或資料流程必須支援讀取、寫入和搜尋。When you set the mode to Update, the underlying file or stream must support reading, writing, and seeking. 整個封存的內容會保留在記憶體中,而且在處置封存之前,不會將任何資料寫入基礎檔案或資料流程。The content of the entire archive is held in memory, and no data is written to the underlying file or stream until the archive is disposed.

下列方法包含名為 mode 的參數,可讓您指定封存模式:The following methods include a parameter named mode that lets you specify the archive mode:

適用於