ZipFileExtensions.ExtractToFile 方法

定义

将 zip 存档中的条目解压到文件下。

重载

ExtractToFile(ZipArchiveEntry, String)

将 zip 存档中的条目解压到文件下。

ExtractToFile(ZipArchiveEntry, String, Boolean)

将 zip 存档中的条目解压缩到文件下,并可选择覆盖具有相同名称的现有文件。

ExtractToFile(ZipArchiveEntry, String)

Source:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Source:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Source:
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)

Source:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Source:
ZipFileExtensions.ZipArchiveEntry.Extract.cs
Source:
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 请改用 方法。

适用于