Bagikan melalui


ZipArchiveEntry.FullName Properti

Definisi

Mendapatkan jalur relatif entri dalam arsip zip.

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

Nilai Properti

Jalur relatif entri dalam arsip zip.

Keterangan

Properti FullName berisi jalur relatif, termasuk hierarki subdirektori, entri dalam arsip zip. (Sebaliknya, Name properti hanya berisi nama entri dan tidak menyertakan hierarki subdirektori.) Misalnya, jika Anda membuat dua entri dalam arsip zip dengan menggunakan CreateEntryFromFile metode dan menyediakan NewEntry.txt sebagai nama untuk entri pertama dan AddedFolder\\NewEntry.txt untuk entri kedua, kedua entri akan memiliki NewEntry.txt di Name properti . Entri pertama juga akan memiliki NewEntry.txt di FullName properti , tetapi entri kedua akan memiliki AddedFolder\\NewEntry.txt di FullName properti .

Anda dapat menentukan string apa pun sebagai jalur entri, termasuk string yang menentukan jalur tidak valid dan absolut. Oleh karena itu, FullName properti mungkin berisi nilai yang tidak diformat dengan benar. Jalur yang tidak valid atau absolut dapat mengakibatkan pengecualian saat Anda mengekstrak konten arsip zip.

Contoh

Contoh berikut menunjukkan cara melakukan iterasi melalui konten file .zip, dan mengekstrak file yang berisi ekstensi .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

Berlaku untuk