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.

示例

下面的示例演示如何在ZipArchiveMode ZipArchive创建对象时指定一个值。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:

适用于