ZipArchiveEntry.FullName 属性

定义

获取 zip 存档中的项的相对路径。

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

属性值

String

在 zip 存档中的项的相对路径。

注解

FullName 属性包含 zip 存档中条目的相对路径(包括子目录层次结构)。 (相反,该Name属性仅包含条目的名称,并且不包含子目录层次结构。) 例如,如果使用该方法在 zip 存档CreateEntryFromFile中创建两个条目,并且NewEntry.txt提供第一个条目的名称和第AddedFolder\\NewEntry.txt二个条目,则这两个条目都将在属性中Name具有NewEntry.txt。 第一个条目也将在属性中FullName具有NewEntry.txt,但第二个条目将在属性中FullName具有AddedFolder\\NewEntry.txt

可以将任何字符串指定为条目的路径,包括指定无效路径和绝对路径的字符串。 因此,该 FullName 属性可能包含格式不正确的值。 提取 zip 存档的内容时,无效或绝对路径可能会导致异常。

示例

以下示例演示如何循环访问.zip文件的内容,以及如何提取包含.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

适用于