ZipArchiveEntry.FullName 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 zip 存档中的项的相对路径。
public:
property System::String ^ FullName { System::String ^ get(); };
public string FullName { get; }
member this.FullName : string
Public ReadOnly Property FullName As String
属性值
在 zip 存档中的项的相对路径。
注解
属性 FullName 包含 zip 存档中某个条目的相对路径(包括子目录层次结构)。 (相反,Name属性仅包含条目的名称,不包含子目录 hierarchy。) 例如,如果使用 方法在 zip 存档CreateEntryFromFile中创建两个条目,并为第一个条目和第AddedFolder\\NewEntry.txt
二个条目提供 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