ZipArchiveEntry.FullName Proprietà

Definizione

Ottiene il percorso relativo della voce nell'archivio ZIP.

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

Valore della proprietà

String

Percorso relativo della voce nell'archivio ZIP.

Commenti

La FullName proprietà contiene il percorso relativo, inclusa la gerarchia di sottodirectory, di una voce in un archivio ZIP. Al contrario, la Name proprietà contiene solo il nome della voce e non include la gerarchia di sottodirectory. Ad esempio, se si creano due voci in un archivio ZIP usando il CreateEntryFromFile metodo e si specifica NewEntry.txt come nome per la prima voce e AddedFolder\\NewEntry.txt per la seconda voce, entrambe le voci avranno NewEntry.txt nella Name proprietà . La prima voce avrà NewEntry.txt anche nella FullName proprietà , ma la seconda voce avrà AddedFolder\\NewEntry.txt nella FullName proprietà .

È possibile specificare qualsiasi stringa come percorso di una voce, incluse le stringhe che specificano percorsi non validi e assoluti. Pertanto, la FullName proprietà potrebbe contenere un valore non formattato correttamente. Un percorso non valido o assoluto può causare un'eccezione quando si estrae il contenuto dell'archivio ZIP.

Esempio

Nell'esempio seguente viene illustrato come scorrere il contenuto di un file .zip ed estrarre i file che contengono l'estensione .txt.

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

Si applica a