ZipFileExtensions 类

定义

为和ZipArchive类提供扩展方法ZipArchiveEntry

public ref class ZipFileExtensions abstract sealed
public static class ZipFileExtensions
type ZipFileExtensions = class
Public Module ZipFileExtensions
继承
ZipFileExtensions

注解

ZipFileExtensions 类仅包含扩展 ZipArchiveZipArchiveEntry 类的静态方法。 您不创建类的ZipFileExtensions实例;而是从或ZipArchiveZipArchiveEntry实例使用这些方法。

若要使用扩展方法,必须在项目中引用 System.IO.Compression.FileSystem 程序集。 Windows 8.x 应用商店应用中不提供 System.IO.Compression.FileSystem 程序集。 因此,ZipFileExtensionsZipFile 类(它们都位于 System.IO.Compression.FileSystem 程序集中)在 Windows 8.x 应用商店应用中不可用。 在 Windows 8.x 应用商店应用中,可以使用 ZipArchiveZipArchiveEntryDeflateStreamGZipStream 中的方法处理压缩文件。

ZipFileExtensions 类包含四个扩展 ZipArchive方法:

ZipFileExtensions 类包含两个扩展 ZipArchiveEntry方法:

示例

以下示例演示如何从现有文件在 zip 存档中创建一个新条目,以及如何将存档的内容提取到目录中。

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\users\exampleuser\end.zip"
        Dim extractPath As String = "c:\users\exampleuser\extract"
        Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
            archive.ExtractToDirectory(extractPath)
        End Using
    End Sub

End Module

以下示例演示如何循环访问 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

方法

名称 说明
CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

通过使用指定的压缩级别压缩文件并将其添加到 zip 存档来存档文件。

CreateEntryFromFile(ZipArchive, String, String)

通过压缩文件并将其添加到 zip 存档来存档文件。

CreateEntryFromFileAsync(ZipArchive, String, String, CancellationToken)

以异步方式将文件系统中的文件添加到指定条目名称下的存档。

CreateEntryFromFileAsync(ZipArchive, String, String, CompressionLevel, CancellationToken)

以异步方式将文件系统中的文件添加到指定条目名称下的存档。

ExtractToDirectory(ZipArchive, String, Boolean)

将存档中的所有文件提取到文件系统上的目录。

ExtractToDirectory(ZipArchive, String)

将 zip 存档中的所有文件提取到文件系统上的目录。

ExtractToDirectoryAsync(ZipArchive, String, Boolean, CancellationToken)

将存档中的所有文件提取到文件系统上的目录。 指定的目录可能已存在。

ExtractToDirectoryAsync(ZipArchive, String, CancellationToken)

将存档中的所有文件异步提取到文件系统上的目录。 指定的目录已存在。

ExtractToFile(ZipArchiveEntry, String, Boolean)

将 zip 存档中的条目提取到文件中,并选择性地覆盖具有相同名称的现有文件。

ExtractToFile(ZipArchiveEntry, String)

将 zip 存档中的条目提取到文件中。

ExtractToFileAsync(ZipArchiveEntry, String, Boolean, CancellationToken)

使用条目的内容和指定名称异步在文件系统上创建文件。

ExtractToFileAsync(ZipArchiveEntry, String, CancellationToken)

使用条目的内容和指定名称异步在文件系统上创建文件。

适用于