共用方式為


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 與使用序數比較進行比較。

適用於