ZipFile.CreateFromDirectory ZipFile.CreateFromDirectory ZipFile.CreateFromDirectory ZipFile.CreateFromDirectory Method

定义

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

重载

CreateFromDirectory(String, String) CreateFromDirectory(String, String) CreateFromDirectory(String, String) CreateFromDirectory(String, String)

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

CreateFromDirectory(String, String, CompressionLevel, Boolean) CreateFromDirectory(String, String, CompressionLevel, Boolean) 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) CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding) 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.

CreateFromDirectory(String, String) CreateFromDirectory(String, String) CreateFromDirectory(String, String) CreateFromDirectory(String, String)

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

public:
 static void CreateFromDirectory(System::String ^ sourceDirectoryName, System::String ^ destinationArchiveFileName);
public static void CreateFromDirectory (string sourceDirectoryName, string destinationArchiveFileName);
static member CreateFromDirectory : string * string -> unit
Public Shared Sub CreateFromDirectory (sourceDirectoryName As String, destinationArchiveFileName As String)

参数

sourceDirectoryName
String String String String

到要存档的目录的路径,指定为相对路径或绝对路径。The path to the directory to be archived, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

destinationArchiveFileName
String String String String

要生成的存档路径,指定为相对路径或绝对路径。The path of the archive to be created, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

异常

sourceDirectoryNamedestinationArchiveFileNameEmpty,仅包含空格,或包含至少一个无效字符。sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

sourceDirectoryNamedestinationArchiveFileNamenullsourceDirectoryName or destinationArchiveFileName is null.

sourceDirectoryNamedestinationArchiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName 无效或不存在(例如,在未映射的驱动器上)。sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName 已存在。destinationArchiveFileName already exists.

-or- 未能打开指定目录中的文件。A file in the specified directory could not be opened.

destinationArchiveFileName 指定目录。destinationArchiveFileName specifies a directory.

-or- 调用方不具有访问在 sourceDirectoryName 中指定的目录或在 destinationArchiveFileName 中指定的文件的所需权限。The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryNamedestinationArchiveFileName 包含的格式无效。sourceDirectoryName or destinationArchiveFileName contains an invalid format.

- 或 --or- zip 存档不支持写入。The zip archive does not support writing.

示例

此示例演示如何使用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

注解

文件系统中的目录结构将保留在存档中。The directory structure from the file system is preserved in the archive. 如果目录为空, 则将创建一个空的存档。If the directory is empty, an empty archive is created. 此方法重载不包括存档中的基目录, 并且不允许指定压缩级别。This method overload does not include the base directory in the archive and does not allow you to specify a compression level. 如果要包括基目录或指定压缩级别, 请调用CreateFromDirectory(String, String, CompressionLevel, Boolean)方法重载。If you want to include the base directory or specify a compression level, call the CreateFromDirectory(String, String, CompressionLevel, Boolean) method overload.

如果存档已存在, IOException则会引发异常。If the archive already exists, an IOException exception is thrown. 如果存档中已经存在具有指定名称的条目, 则将使用相同的名称创建另一个条目。If an entry with the specified name already exists in the archive, a second entry is created with an identical name.

如果无法将目录中的某个文件添加到存档, 则存档将不完整并且无效, 并且该方法将引发IOException异常。If a file in the directory cannot be added to the archive, the archive is left incomplete and invalid, and the method throws an IOException exception.

CreateFromDirectory(String, String, CompressionLevel, Boolean) CreateFromDirectory(String, String, CompressionLevel, Boolean) 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.

public:
 static void CreateFromDirectory(System::String ^ sourceDirectoryName, System::String ^ destinationArchiveFileName, System::IO::Compression::CompressionLevel compressionLevel, bool includeBaseDirectory);
public static void CreateFromDirectory (string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory);
static member CreateFromDirectory : string * string * System.IO.Compression.CompressionLevel * bool -> unit

参数

sourceDirectoryName
String String String String

到要存档的目录的路径,指定为相对路径或绝对路径。The path to the directory to be archived, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

destinationArchiveFileName
String String String String

要生成的存档路径,指定为相对路径或绝对路径。The path of the archive to be created, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

compressionLevel
CompressionLevel CompressionLevel CompressionLevel CompressionLevel

指示创建项时是否强调速度或压缩有效性的枚举值之一。One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean Boolean Boolean Boolean

包括从在存档的根的 sourceDirectoryName 的目录名称,则为 true;仅包含目录中的内容,则为 falsetrue to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

异常

sourceDirectoryNamedestinationArchiveFileNameEmpty,仅包含空格,或包含至少一个无效字符。sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

sourceDirectoryNamedestinationArchiveFileNamenullsourceDirectoryName or destinationArchiveFileName is null.

sourceDirectoryNamedestinationArchiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName 无效或不存在(例如,在未映射的驱动器上)。sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName 已存在。destinationArchiveFileName already exists.

-or- 未能打开指定目录中的文件。A file in the specified directory could not be opened.

destinationArchiveFileName 指定目录。destinationArchiveFileName specifies a directory.

- 或 --or- 调用方不具有访问在 sourceDirectoryName 中指定的目录或在 destinationArchiveFileName 中指定的文件的所需权限。The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryNamedestinationArchiveFileName 包含的格式无效。sourceDirectoryName or destinationArchiveFileName contains an invalid format.

- 或 --or- zip 存档不支持写入。The zip archive does not support writing.

示例

此示例演示如何使用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. 压缩存档时, 将包括基目录, 并设置压缩级别以强调操作的速度, 从而提高效率。When compressing the archive, the base directory is included and the compression level is set to emphasize the speed of the operation over efficiency. 若要使用 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;
using System.IO.Compression;

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

            ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);

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

Module Module1

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

        ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, True)

        ZipFile.ExtractToDirectory(zipPath, extractPath)
    End Sub

End Module

注解

文件系统中的目录结构将保留在存档中。The directory structure from the file system is preserved in the archive. 如果目录为空, 则将创建一个空的存档。If the directory is empty, an empty archive is created. 使用此方法重载可指定压缩级别, 并指定是否在存档中包括基目录。Use this method overload to specify the compression level and whether to include the base directory in the archive.

如果存档已存在, IOException则会引发异常。If the archive already exists, an IOException exception is thrown. 如果存档中已经存在具有指定名称的条目, 则将使用相同的名称创建另一个条目。If an entry with the specified name already exists in the archive, a second entry is created with an identical name.

如果无法将目录中的某个文件添加到存档, 则存档将不完整并且无效, 并且该方法将引发IOException异常。If a file in the directory cannot be added to the archive, the archive is left incomplete and invalid, and the method throws an IOException exception.

CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding) CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding) 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.

public:
 static void CreateFromDirectory(System::String ^ sourceDirectoryName, System::String ^ destinationArchiveFileName, System::IO::Compression::CompressionLevel compressionLevel, bool includeBaseDirectory, System::Text::Encoding ^ entryNameEncoding);
public static void CreateFromDirectory (string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Text.Encoding entryNameEncoding);
static member CreateFromDirectory : string * string * System.IO.Compression.CompressionLevel * bool * System.Text.Encoding -> unit

参数

sourceDirectoryName
String String String String

到要存档的目录的路径,指定为相对路径或绝对路径。The path to the directory to be archived, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

destinationArchiveFileName
String String String String

要生成的存档路径,指定为相对路径或绝对路径。The path of the archive to be created, specified as a relative or absolute path. 相对路径被解释为相对于当前工作目录。A relative path is interpreted as relative to the current working directory.

compressionLevel
CompressionLevel CompressionLevel CompressionLevel CompressionLevel

指示创建项时是否强调速度或压缩有效性的枚举值之一。One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean Boolean Boolean Boolean

包括从在存档的根的 sourceDirectoryName 的目录名称,则为 true;仅包含目录中的内容,则为 falsetrue to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

entryNameEncoding
Encoding Encoding Encoding Encoding

在存档中读取或写入项名时使用的编码。The encoding to use when reading or writing entry names in this archive. 仅当需要针对具有不支持条目名称的 UTF-8 编码的 zip 归档工具和库的互操作性进行编码时,为此参数指定值。Specify a value for this parameter only when an encoding is required for interoperability with zip archive tools and libraries that do not support UTF-8 encoding for entry names.

异常

sourceDirectoryNamedestinationArchiveFileNameEmpty,仅包含空格,或包含至少一个无效字符。sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

-or- entryNameEncoding 设置为不同于 UTF-8 的 Unicode 编码。entryNameEncoding is set to a Unicode encoding other than UTF-8.

sourceDirectoryNamedestinationArchiveFileNamenullsourceDirectoryName or destinationArchiveFileName is null.

sourceDirectoryNamedestinationArchiveFileName 内,指定的路径、文件名或者两者都超出了系统定义的最大长度。In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName 无效或不存在(例如,在未映射的驱动器上)。sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName 已存在。destinationArchiveFileName already exists.

-or- 未能打开指定目录中的文件。A file in the specified directory could not be opened.

destinationArchiveFileName 指定目录。destinationArchiveFileName specifies a directory.

-or- 调用方不具有访问在 sourceDirectoryName 中指定的目录或在 destinationArchiveFileName 中指定的文件的所需权限。The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryNamedestinationArchiveFileName 包含的格式无效。sourceDirectoryName or destinationArchiveFileName contains an invalid format.

-or- zip 存档不支持写入。The zip archive does not support writing.

注解

文件系统中的目录结构将保留在存档中。The directory structure from the file system is preserved in the archive. 如果目录为空, 则将创建一个空的存档。If the directory is empty, an empty archive is created. 使用此方法重载可指定压缩级别和字符编码, 以及是否在存档中包括基目录。Use this method overload to specify the compression level and character encoding, and whether to include the base directory in the archive.

如果存档已存在, IOException则会引发异常。If the archive already exists, an IOException exception is thrown. 如果存档中已经存在具有指定名称的条目, 则将使用相同的名称创建另一个条目。If an entry with the specified name already exists in the archive, a second entry is created with an identical name.

如果无法将目录中的某个文件添加到存档, 则存档将不完整并且无效, 并且该方法将引发IOException异常。If a file in the directory cannot be added to the archive, the archive is left incomplete and invalid, and the method throws an IOException exception.

如果entryNameEncoding将设置为以外的值null, 则将使用指定的编码对条目名称进行编码。If entryNameEncoding is set to a value other than null, the entry names are encoded by using the specified encoding. 如果指定的编码为 UTF-8, 则为每个条目设置语言编码标志 (本地文件头的通用位标志)。If the specified encoding is a UTF-8, the language encoding flag (in the general-purpose bit flag of the local file header) is set for each entry,

如果entryNameEncoding设置为null, 则根据以下规则对条目名称进行编码:If entryNameEncoding is set to null, the entry names are encoded according to the following rules:

  • 对于包含 ASCII 范围外的字符的条目名称, 设置语言编码标志, 并使用 UTF-8 对条目名称进行编码。For entry names that contain characters outside the ASCII range, the language encoding flag is set, and UTF-8 is used to encode the entry name.

  • 对于仅包含 ASCII 字符的条目名称, 会设置语言编码标志, 当前系统默认代码页用于对条目名称进行编码。For entry names that contain only ASCII characters, the language encoding flag is set, and the current system default code page is used to encode the entry names.

适用于