ZipFileExtensions.ExtractToFile 方法

定義

將 zip 封存中的項目解壓縮至檔案。

多載

ExtractToFile(ZipArchiveEntry, String)

將 zip 封存中的項目解壓縮至檔案。

ExtractToFile(ZipArchiveEntry, String, Boolean)

擷取檔案 zip 封存中的項目,並選擇性地覆寫具有相同名稱的現有檔案。

ExtractToFile(ZipArchiveEntry, String)

來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

將 zip 封存中的項目解壓縮至檔案。

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToFile(System::IO::Compression::ZipArchiveEntry ^ source, System::String ^ destinationFileName);
public static void ExtractToFile (this System.IO.Compression.ZipArchiveEntry source, string destinationFileName);
static member ExtractToFile : System.IO.Compression.ZipArchiveEntry * string -> unit
<Extension()>
Public Sub ExtractToFile (source As ZipArchiveEntry, destinationFileName As String)

參數

source
ZipArchiveEntry

要從中解壓縮檔案的 zip 封存項目。

destinationFileName
String

要從中建立項目內容的檔案路徑。 您可以指定相對或絕對路徑。 相對路徑會解譯為與目前的工作目錄相對。

例外狀況

destinationFileName 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。

-或-

destinationFileName 會指定目錄。

destinationFileNamenull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

destinationFileName 已經存在。

-或-

發生 I/O 錯誤。

-或-

項目目前開啟供寫入。

-或-

已從封存刪除項目。

呼叫端不具所需的權限,無法建立新的檔案。

此項目可能是從封存中遺失,或已損毀且無法讀取。

-或-

已使用不受支援的壓縮方法壓縮項目。

已處置此項目所屬的 zip 封存。

destinationFileName 格式無效。

-或-

這個項目的 zip 封存已經以 Create 模式開啟,而該模式不允許擷取項目。

範例

下列範例示範如何逐一查看 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

備註

如果目的地檔案已經存在,則這個方法不會覆寫它;它會擲回例外狀況 IOException 。 若要覆寫現有的檔案,請改用 ExtractToFile(ZipArchiveEntry, String, Boolean) 方法多載。

檔案的最後一次寫入時間會設定為 ZIP 封存中的專案上次變更的時間;這個值會儲存在屬性中 LastWriteTime

您無法使用此方法來擷取目錄;請改用 ExtractToDirectory 方法。

適用於

ExtractToFile(ZipArchiveEntry, String, Boolean)

來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
來源:
ZipFileExtensions.ZipArchiveEntry.Extract.cs

擷取檔案 zip 封存中的項目,並選擇性地覆寫具有相同名稱的現有檔案。

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToFile(System::IO::Compression::ZipArchiveEntry ^ source, System::String ^ destinationFileName, bool overwrite);
public static void ExtractToFile (this System.IO.Compression.ZipArchiveEntry source, string destinationFileName, bool overwrite);
static member ExtractToFile : System.IO.Compression.ZipArchiveEntry * string * bool -> unit
<Extension()>
Public Sub ExtractToFile (source As ZipArchiveEntry, destinationFileName As String, overwrite As Boolean)

參數

source
ZipArchiveEntry

要從中解壓縮檔案的 zip 封存項目。

destinationFileName
String

要從中建立項目內容的檔案路徑。 您可以指定相對或絕對路徑。 相對路徑會解譯為與目前的工作目錄相對。

overwrite
Boolean

若要覆寫與目的地檔案同名的現有檔案,則為 true;否則為 false

例外狀況

destinationFileName 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。

-或-

destinationFileName 會指定目錄。

destinationFileNamenull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

destinationFileName 已存在,且 overwritefalse

-或-

發生 I/O 錯誤。

-或-

項目目前開啟供寫入。

-或-

已從封存刪除項目。

呼叫端不具所需的權限,無法建立新的檔案。

封存遺漏項目,或是項目已損毀且無法讀取。

-或-

已使用不受支援的壓縮方法壓縮項目。

已處置此項目所屬的 zip 封存。

destinationFileName 格式無效。

-或-

這個項目的 zip 封存已經以 Create 模式開啟,而該模式不允許擷取項目。

範例

下列範例示範如何逐一查看 zip 封存盤案的內容,以及擷取擴展名為 .txt 的檔案。 它會覆寫目的地資料夾中具有相同名稱的現有檔案。 若要編譯此程式代碼範例,您必須參考專案中的 System.IO.CompressionSystem.IO.Compression.FileSystem 元件。

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\example\start.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))
                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, true);
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\example\start.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) 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, true)
                    End If

                End If
            Next
        End Using
    End Sub

End Module

備註

檔案的最後一次寫入時間會設定為 ZIP 封存中的專案上次變更的時間;這個值會儲存在屬性中 LastWriteTime

您無法使用此方法來擷取目錄;請改用 ExtractToDirectory 方法。

適用於