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

プロパティ値

zip アーカイブ内のエントリの相対パス。

注釈

プロパティには FullName 、zip アーカイブ内のエントリのサブディレクトリ階層を含む相対パスが含まれます。 (これに対しName、 プロパティにはエントリの名前のみが含まれており、サブディレクトリ階層は含まれません)。たとえば、 メソッドを使用CreateEntryFromFileして zip アーカイブに 2 つのエントリを作成し、最初のエントリと AddedFolder\\NewEntry.txt 2 番目のエントリの名前として を指定NewEntry.txtすると、両方のエントリが NewEntry.txt プロパティにName含まれます。 最初のエントリは プロパティにも 含NewEntry.txtFullNameまれますが、2 番目のエントリは AddedFolder\\NewEntry.txt プロパティにありますFullName

無効なパスと絶対パスを指定する文字列など、エントリのパスとして任意の文字列を指定できます。 したがって、プロパティに 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

適用対象