ZipArchive.GetEntry(String) 方法

定义

在 zip 存档中检索指定项的包装。

public:
 System::IO::Compression::ZipArchiveEntry ^ GetEntry(System::String ^ entryName);
public System.IO.Compression.ZipArchiveEntry GetEntry (string entryName);
public System.IO.Compression.ZipArchiveEntry? GetEntry (string entryName);
member this.GetEntry : string -> System.IO.Compression.ZipArchiveEntry
Public Function GetEntry (entryName As String) As ZipArchiveEntry

参数

entryName
String

相对于存档的根的路径标识了项目检索。

返回

存档中指定项的包装器;如果存档中不存在此项,则为 null

例外

entryNameEmpty

entryNamenull

zip 存档不支持读取。

zip 存档已释放。

zip 存档已损坏,不能检索其项。

示例

以下示例演示如何使用 GetEntry 方法检索条目。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\example\result.zip";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                ZipArchiveEntry entry = archive.GetEntry("ExistingFile.txt");
                using (StreamWriter writer = new StreamWriter(entry.Open()))
                {
                    writer.BaseStream.Seek(0, SeekOrigin.End);
                    writer.WriteLine("append line to file");
                }
                entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime;
            }
        }
    }
}
open System
open System.IO
open System.IO.Compression

[<EntryPoint>]
let main _ =
    let zipPath = @"c:\example\result.zip"

    use archive = ZipFile.Open(zipPath, ZipArchiveMode.Update)

    let entry = archive.GetEntry "ExistingFile.txt"
    use writer = new StreamWriter(entry.Open())

    writer.BaseStream.Seek(0, SeekOrigin.End) |> ignore
    writer.WriteLine "append line to file"

    entry.LastWriteTime <- DateTimeOffset.UtcNow.LocalDateTime
    0
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\example\result.zip"

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            Dim entry As ZipArchiveEntry = archive.GetEntry("ExistingFile.txt")

            Using writer As StreamWriter = New StreamWriter(entry.Open())
                writer.BaseStream.Seek(0, SeekOrigin.End)
                writer.WriteLine("append line to file")
            End Using
            entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime
        End Using
    End Sub

End Module

注解

如果存档中存在多个具有指定名称的条目,则返回第一个条目。 条目的名称与使用序号比较 entryName 进行比较。

适用于