ZipArchiveEntry.FullName Eigenschaft

Definition

Ruft den relativen Pfad des Eintrags im ZIP-Archiv ab.

public:
 property System::String ^ FullName { System::String ^ get(); };
public string FullName { get; }
member this.FullName : string
Public ReadOnly Property FullName As String

Eigenschaftswert

String

Der relative Pfad des Eintrags im Zip-Archiv.

Hinweise

Die FullName Eigenschaft enthält den relativen Pfad, einschließlich der Unterverzeichnishierarchie, eines Eintrags in einem ZIP-Archiv. (Im Gegensatz dazu enthält die Name Eigenschaft nur den Namen des Eintrags und enthält nicht die Unterverzeichnishierarchie.) Wenn Sie beispielsweise zwei Einträge in einem Zip-Archiv mithilfe CreateEntryFromFile der Methode erstellen und als Name für den ersten Eintrag und AddedFolder\\NewEntry.txt für den zweiten Eintrag angebenNewEntry.txt, verfügen NewEntry.txt beide Einträge in der Name Eigenschaft. Der erste Eintrag hat auch NewEntry.txt in der Eigenschaft, aber der zweite Eintrag AddedFolder\\NewEntry.txt hat in der FullName FullName Eigenschaft.

Sie können eine beliebige Zeichenfolge als Pfad eines Eintrags angeben, einschließlich Zeichenfolgen, die ungültige und absolute Pfade angeben. Daher kann die Eigenschaft einen Wert enthalten, der FullName nicht ordnungsgemäß formatiert ist. Ein ungültiger oder absoluter Pfad kann zu einer Ausnahme führen, wenn Sie den Inhalt des ZIP-Archivs extrahieren.

Beispiele

Im folgenden Beispiel wird gezeigt, wie Sie den Inhalt einer .zip-Datei durchlaufen und Dateien extrahieren, die die .txt-Erweiterung enthalten.

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

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

        Console.WriteLine("Provide path where to extract the zip file:");
        string extractPath = Console.ReadLine();

        // Normalizes the path.
        extractPath = Path.GetFullPath(extractPath);

        // Ensures that the last character on the extraction path
        // is the directory separator char.
        // Without this, a malicious zip file could try to traverse outside of the expected
        // extraction path.
        if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            extractPath += Path.DirectorySeparatorChar;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    // Gets the full path to ensure that relative segments are removed.
                    string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                    // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    // are case-insensitive.
                    if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                        entry.ExtractToFile(destinationPath);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

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

        Console.WriteLine("Provide path where to extract the zip file:")
        Dim extractPath As String = Console.ReadLine()

        ' Normalizes the path.
        extractPath = Path.GetFullPath(extractPath)

        ' Ensures that the last character on the extraction path
        ' is the directory separator char. 
        ' Without this, a malicious zip file could try to traverse outside of the expected
        ' extraction path.
        If Not extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) Then
            extractPath += Path.DirectorySeparatorChar
        End If

        Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
            For Each entry As ZipArchiveEntry In archive.Entries
                If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then

                    ' Gets the full path to ensure that relative segments are removed.
                    Dim destinationPath As String = Path.GetFullPath(Path.Combine(extractPath, entry.FullName))
                    
                    ' Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                    ' are case-insensitive.
                    If destinationPath.StartsWith(extractPath, StringComparison.Ordinal) Then 
                        entry.ExtractToFile(destinationPath)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

Gilt für