ZipFile 类

定义

提供静态方法,用于创建、提取和打开压缩文档。Provides static methods for creating, extracting, and opening zip archives.

public ref class ZipFile abstract sealed
public static class ZipFile
type ZipFile = class
Public Class ZipFile
继承
ZipFile

示例

此示例演示如何使用ZipFile类创建和提取 zip 存档。This example shows how to create and extract a zip archive by using the ZipFile class. 它将文件夹的内容压缩为 zip 存档, 然后将该内容提取到新文件夹中。It compresses the contents of a folder into a zip archive, and then extracts that content to a new folder.

提示

若要使用 ZipFile 类,必须在项目中引用 System.IO.Compression.FileSystem 程序集。To use the ZipFile class, you must reference the System.IO.Compression.FileSystem assembly in your project.

using System;
using System.IO.Compression;

class Program
{
    static void Main(string[] args)
    {
        string startPath = @".\start";
        string zipPath = @".\result.zip";
        string extractPath = @".\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim startPath As String = ".\start"
        Dim zipPath As String = ".\result.zip"
        Dim extractPath As String = ".\extract"

        ZipFile.CreateFromDirectory(startPath, zipPath)

        ZipFile.ExtractToDirectory(zipPath, extractPath)
    End Sub

End Module

注解

重要

若要使用ZipFile类, 必须在项目中添加System.IO.Compression.FileSystem对程序集的引用; 否则, 在尝试编译时, 会收到以下错误消息:当前上下文中不存在名称 "ZipFile"To use the ZipFile class, you must add a reference to the System.IO.Compression.FileSystem assembly in your project; otherwise, you'll get the following error message when trying to compile : The name 'ZipFile' does not exist in the current context. 有关如何在 Visual Studio 中添加对项目的引用的详细信息, 请参阅如何:使用引用管理器添加或删除引用For more information on how to add a reference to your project in Visual Studio, see How to: Add or Remove References By Using the Reference Manager.

操作 zip 存档及其文件的方法分布于三个类: ZipFileZipArchiveZipArchiveEntryThe methods for manipulating zip archives and their files are spread across three classes: ZipFile, ZipArchive and ZipArchiveEntry.

自...To… 使用...Use…
从目录创建 zip 存档Create a zip archive from a directory ZipFile.CreateFromDirectory
将 zip 存档的内容提取到目录Extract the contents of a zip archive to a directory ZipFile.ExtractToDirectory
将新文件添加到现有 zip 存档Add new files to an existing zip archive ZipArchive.CreateEntry
在 zip 存档中检索文件Retrieve an file in a zip archive ZipArchive.GetEntry
检索 zip 存档中的所有文件Retrieve all of the files in a zip archive ZipArchive.Entries
打开 zip 存档中包含的单个文件的流To open a stream to an individual file contained in a zip archive ZipArchiveEntry.Open
删除 zip 存档中的文件Delete a file from a zip archive ZipArchiveEntry.Delete

不能在应用ZipFileZipFileExtensions Windows 8.x 应用商店Windows 8.x Store使用或类。You cannot use the ZipFile or ZipFileExtensions classes in Windows 8.x 应用商店Windows 8.x Store apps. Windows 8.x 应用商店Windows 8.x Store应用程序中, 应使用以下类来处理压缩的文件。In Windows 8.x 应用商店Windows 8.x Store apps, you should use the following classes to work with compressed files.

方法

CreateFromDirectory(String, String)

创建 zip 存档,该存档包含文件和指定目录的目录。Creates a zip archive that contains the files and directories from the specified directory.

CreateFromDirectory(String, String, CompressionLevel, Boolean)

创建 zip 存档,该存档包括指定目录的文件和目录,使用指定压缩级别,以及可以选择包含基目录。Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level, and optionally includes the base directory.

CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding)

创建 zip 存档,该存档包括文件和指定目录的目录,使用指定压缩级别和条目名称的字符编码,以及可以选择包含基目录。Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names, and optionally includes the base directory.

ExtractToDirectory(String, String)

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

ExtractToDirectory(String, String, Boolean)

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

ExtractToDirectory(String, String, Encoding)

将指定 zip 存档中的所有文件解压缩到文件系统的一目录下并指定的条目名称字符编码。Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names.

ExtractToDirectory(String, String, Encoding, Boolean)

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

Open(String, ZipArchiveMode)

以指定的模式打开指定路径上的 zip 归档。Opens a zip archive at the specified path and in the specified mode.

Open(String, ZipArchiveMode, Encoding)

在指定的模式中用指定的项名称汉字解码打开指定路径的 zip 存档。Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.

OpenRead(String)

打开在指定路径用于读取的 zip 存档。Opens a zip archive for reading at the specified path.

适用于

另请参阅