ZipFileExtensions 类

定义

ZipArchiveZipArchiveEntry 类提供扩展方法。Provides extension methods for the ZipArchive and ZipArchiveEntry classes.

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

示例

下面的示例演示如何从现有文件在 zip 存档中创建新条目,并将存档内容提取到目录中。The following example shows how to create a new entry in a zip archive from an existing file, and extract the contents of the archive to a directory.

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 的文件。The following example shows how to iterate through the contents of a zip archive and extract files that have a .txt extension.

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

注解

ZipFileExtensions 类只包含扩展 ZipArchiveZipArchiveEntry 类的静态方法。The ZipFileExtensions class contains only static methods that extend the ZipArchive and ZipArchiveEntry classes. 不创建 ZipFileExtensions 类的实例;相反,请从 ZipArchiveZipArchiveEntry的实例使用这些方法。You do not create an instance of the ZipFileExtensions class; instead, you use these methods from instances of ZipArchive or ZipArchiveEntry.

若要使用扩展方法,必须在项目中引用 System.IO.Compression.FileSystem 程序集。To use the extension methods, you must reference the System.IO.Compression.FileSystem assembly in your project. System.IO.Compression.FileSystem 程序集在 Windows 8.x 应用商店Windows 8.x Store 应用程序中不可用。The System.IO.Compression.FileSystem assembly is not available in Windows 8.x 应用商店Windows 8.x Store apps. 因此,ZipFileExtensionsZipFile 类(两者都在 System.IO.Compression.FileSystem 程序集中)在 Windows 8.x 应用商店Windows 8.x Store 应用程序中不可用。Therefore, the ZipFileExtensions and ZipFile classes (both of which are in the System.IO.Compression.FileSystem assembly) are not available in Windows 8.x 应用商店Windows 8.x Store apps. Windows 8.x 应用商店Windows 8.x Store 应用程序中,可以使用 ZipArchiveZipArchiveEntryDeflateStreamGZipStream中的方法来处理压缩的文件。In Windows 8.x 应用商店Windows 8.x Store apps, you work with compressed files by using the methods in ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream.

ZipFileExtensions 类包含用于扩展 ZipArchive的四个方法:The ZipFileExtensions class contains four methods that extend ZipArchive:

ZipFileExtensions 类包含扩展 ZipArchiveEntry的两个方法:The ZipFileExtensions class contains two methods that extend ZipArchiveEntry:

方法

CreateEntryFromFile(ZipArchive, String, String)

通过压缩并将其添加到邮编存档的存档文件。Archives a file by compressing it and adding it to the zip archive.

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

通过使用指定压缩级别压缩并将其添加到邮编存档的存档文件。Archives a file by compressing it using the specified compression level and adding it to the zip archive.

ExtractToDirectory(ZipArchive, String)

将 zip 存档中的所有文件都解压缩到文件系统的一个目录下。Extracts all the files in the zip archive to a directory on the file system.

ExtractToDirectory(ZipArchive, String, Boolean)

将存档中的所有文件都解压缩到文件系统的一个目录下。Extracts all of the files in the archive to a directory on the file system.

ExtractToFile(ZipArchiveEntry, String)

将 zip 存档中的条目解压到文件下。Extracts an entry in the zip archive to a file.

ExtractToFile(ZipArchiveEntry, String, Boolean)

将 zip 存档中的条目解压缩到文件下,并可选择覆盖具有相同名称的现有文件。Extracts an entry in the zip archive to a file, and optionally overwrites an existing file that has the same name.

适用于