ZipArchive.GetEntry(String) Metoda

Definicja

Pobiera otokę dla określonego wpisu w archiwum 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

Parametry

entryName
String

Ścieżka względem katalogu głównego archiwum, która identyfikuje wpis do pobrania.

Zwraca

Otoka dla określonego wpisu w archiwum; null jeśli wpis nie istnieje w archiwum.

Wyjątki

entryName to Empty.

entryName to null.

Archiwum zip nie obsługuje odczytu.

Archiwum zip zostało usunięte.

Archiwum zip jest uszkodzone i nie można pobrać jego wpisów.

Przykłady

W poniższym przykładzie pokazano, jak pobrać wpis przy GetEntry użyciu metody .

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

Uwagi

Jeśli w archiwum istnieje wiele wpisów, które mają określoną nazwę, zwracana jest pierwsza. Nazwa wpisu jest porównywana z entryName użyciem porównania porządkowego.

Dotyczy