ZipArchiveEntry.FullName Tulajdonság

Definíció

Lekéri a bejegyzés relatív elérési útját a zip archívumban.

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

Tulajdonság értéke

A bejegyzés relatív elérési útja a zip archívumban.

Példák

Az alábbi példa bemutatja, hogyan lehet iterálni egy .zip fájl tartalmát, és kinyerni a .txt kiterjesztést tartalmazó fájlokat.

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

Megjegyzések

A FullName tulajdonság egy zip-archívumban lévő bejegyzés relatív elérési útját tartalmazza, beleértve az alkönyvtári hierarchiát is. (Ezzel szemben a Name tulajdonság csak a bejegyzés nevét tartalmazza, és nem tartalmazza az alkönyvtári hierarchiát.) Ha például két bejegyzést hoz létre egy zip-archívumban a CreateEntryFromFile metódus használatával, és NewEntry.txt megadja az első és AddedFolder\\NewEntry.txt a második bejegyzés nevét, mindkét bejegyzés NewEntry.txt szerepel a Name tulajdonságban. Az első bejegyzés is NewEntry.txt szerepel a FullName tulajdonságban, de a második bejegyzés a FullName tulajdonságban leszAddedFolder\\NewEntry.txt.

Bármely karakterláncot megadhat egy bejegyzés elérési útjaként, beleértve az érvénytelen és abszolút elérési utakat meghatározó sztringeket is. Ezért előfordulhat, hogy a FullName tulajdonság nem megfelelően formázott értéket tartalmaz. Érvénytelen vagy abszolút elérési út kivételt eredményezhet a zip-archívum tartalmának kinyerésekor.

A következőre érvényes: