ZipFileExtensions 类

定义

ZipArchiveZipArchiveEntry 类提供扩展方法。

C#
public static class ZipFileExtensions
继承
ZipFileExtensions

注解

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

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

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

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

示例

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

C#
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);
            }
        }
    }
}

以下示例演示如何循环访问 zip 存档的内容并提取扩展名为 .txt 的文件。

C#
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);
                }
            }
        }
    }
}

方法

CreateEntryFromFile(ZipArchive, String, String)

通过压缩并将其添加到邮编存档的存档文件。

CreateEntryFromFile(ZipArchive, String, String, CompressionLevel)

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

ExtractToDirectory(ZipArchive, String)

将 zip 存档中的所有文件都解压缩到文件系统的一个目录下。

ExtractToDirectory(ZipArchive, String, Boolean)

将存档中的所有文件都解压缩到文件系统的一个目录下。

ExtractToFile(ZipArchiveEntry, String)

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

ExtractToFile(ZipArchiveEntry, String, Boolean)

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

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0