แก้ไข

แชร์ผ่าน


ZipFile.CreateFromDirectory Method

Definition

Creates a zip archive that contains the files and directories from the specified directory.

Overloads

CreateFromDirectory(String, Stream)

Creates a zip archive in the specified stream that contains the files and directories from the specified directory.

CreateFromDirectory(String, String)

Creates a zip archive that contains the files and directories from the specified directory.

CreateFromDirectory(String, Stream, CompressionLevel, Boolean)

Creates a zip archive in the specified stream 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)

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, Stream, CompressionLevel, Boolean, Encoding)

Creates a zip archive in the specified stream that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names and comments, and optionally includes the base directory.

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

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 comments, and optionally includes the base directory.

CreateFromDirectory(String, Stream)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

Creates a zip archive in the specified stream that contains the files and directories from the specified directory.

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

Parameters

sourceDirectoryName
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.

destination
Stream

The stream where the zip archive is to be stored.

Exceptions

sourceDirectoryName is Empty, contains only white space, or contains at least one invalid character.

-or-

The destination stream does not support writing.

sourceDirectoryName or destination is null.

In sourceDirectoryName the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

sourceDirectoryName contains an invalid format.

Remarks

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. If you want to include the base directory or specify a compression level, call the CreateFromDirectory(String, Stream, CompressionLevel, Boolean) method overload. 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.

Applies to

CreateFromDirectory(String, String)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

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)

Parameters

sourceDirectoryName
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

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.

Exceptions

sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

sourceDirectoryName or destinationArchiveFileName is null.

In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName already exists.

-or-

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

destinationArchiveFileName specifies a directory.

-or-

The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryName or destinationArchiveFileName contains an invalid format.

-or-

The zip archive does not support writing.

Examples

This example shows how to create and extract a zip archive by using the ZipFile class. It compresses the contents of a folder into a zip archive, and then extracts that content to a new folder. 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);
    }
}
open System.IO.Compression

let startPath = @".\start"
let zipPath = @".\result.zip"
let 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

Remarks

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. If you want to include the base directory or specify a compression level, call the CreateFromDirectory(String, String, CompressionLevel, Boolean) method overload.

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.

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.

Applies to

CreateFromDirectory(String, Stream, CompressionLevel, Boolean)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

Creates a zip archive in the specified stream 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::IO::Stream ^ destination, System::IO::Compression::CompressionLevel compressionLevel, bool includeBaseDirectory);
public static void CreateFromDirectory (string sourceDirectoryName, System.IO.Stream destination, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory);
static member CreateFromDirectory : string * System.IO.Stream * System.IO.Compression.CompressionLevel * bool -> unit
Public Shared Sub CreateFromDirectory (sourceDirectoryName As String, destination As Stream, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean)

Parameters

sourceDirectoryName
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.

destination
Stream

The stream where the zip archive is to be stored.

compressionLevel
CompressionLevel

One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean

true to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

Exceptions

sourceDirectoryName is Empty, contains only white space, or contains at least one invalid character.

-or-

The destination stream does not support writing.

sourceDirectoryName or destination is null.

In sourceDirectoryName the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

sourceDirectoryName contains an invalid format.

compressionLevel is not a valid CompressionLevel value.

Remarks

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. 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.

Applies to

CreateFromDirectory(String, String, CompressionLevel, Boolean)

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

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
Public Shared Sub CreateFromDirectory (sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean)

Parameters

sourceDirectoryName
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

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

One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean

true to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

Exceptions

sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

sourceDirectoryName or destinationArchiveFileName is null.

In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName already exists.

-or-

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

destinationArchiveFileName specifies a directory.

-or-

The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryName or destinationArchiveFileName contains an invalid format.

-or-

The zip archive does not support writing.

Examples

This example shows how to create and extract a zip archive by using the ZipFile class. 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. 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);
        }
    }
}
open System.IO.Compression

let startPath = @"c:\example\start"
let zipPath = @"c:\example\result.zip"
let 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

Remarks

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.

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.

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.

Applies to

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

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

Creates a zip archive in the specified stream that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names and comments, and optionally includes the base directory.

public:
 static void CreateFromDirectory(System::String ^ sourceDirectoryName, System::IO::Stream ^ destination, System::IO::Compression::CompressionLevel compressionLevel, bool includeBaseDirectory, System::Text::Encoding ^ entryNameEncoding);
public static void CreateFromDirectory (string sourceDirectoryName, System.IO.Stream destination, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Text.Encoding? entryNameEncoding);
static member CreateFromDirectory : string * System.IO.Stream * System.IO.Compression.CompressionLevel * bool * System.Text.Encoding -> unit
Public Shared Sub CreateFromDirectory (sourceDirectoryName As String, destination As Stream, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, entryNameEncoding As Encoding)

Parameters

sourceDirectoryName
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.

destination
Stream

The stream where the zip archive is to be stored.

compressionLevel
CompressionLevel

One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean

true to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

entryNameEncoding
Encoding

The encoding to use when reading or writing entry names and comments in this archive. 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 or comments.

Exceptions

sourceDirectoryName is Empty, contains only white space, or contains at least one invalid character.

-or-

The destination stream does not support writing.

sourceDirectoryName or destination is null.

In sourceDirectoryName the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

sourceDirectoryName contains an invalid format.

compressionLevel is not a valid CompressionLevel value.

Remarks

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. 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.

Applies to

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

Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs
Source:
ZipFile.Create.cs

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 comments, 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);
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
Public Shared Sub CreateFromDirectory (sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, entryNameEncoding As Encoding)

Parameters

sourceDirectoryName
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

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

One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.

includeBaseDirectory
Boolean

true to include the directory name from sourceDirectoryName at the root of the archive; false to include only the contents of the directory.

entryNameEncoding
Encoding

The encoding to use when reading or writing entry names and comments in this archive. 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 or comments.

Exceptions

sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.

-or-

entryNameEncoding is set to a Unicode encoding other than UTF-8.

sourceDirectoryName or destinationArchiveFileName is null.

In sourceDirectoryName or destinationArchiveFileName, the specified path, file name, or both exceed the system-defined maximum length.

sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).

destinationArchiveFileName already exists.

-or-

A file in the specified directory could not be opened.

-or-

An I/O error occurred while opening a file to be archived.

destinationArchiveFileName specifies a directory.

-or-

The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

sourceDirectoryName or destinationArchiveFileName contains an invalid format.

-or-

The zip archive does not support writing.

Remarks

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.

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.

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.

If entryNameEncoding is set to a value other than null, the entry names and comments are encoded by using the specified encoding. If the specified encoding is a UTF-8 encoding, the language encoding flag (in the general-purpose bit flag of the local file header) is set for each entry.

If entryNameEncoding is set to null, the entry names and comments are encoded according to the following rules:

  • For entry names and comments that contain characters outside the ASCII range, the language encoding flag is set, and UTF-8 is used to encode the entry name and comment.

  • For entry names and comments that contain only ASCII characters, the language encoding flag is not set, and the current system default code page is used to encode the entry names and comments.

Applies to