Redigera

Dela via


ZipFile.CreateFromDirectoryAsync Method

Definition

Overloads

CreateFromDirectoryAsync(String, Stream, CancellationToken)

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

CreateFromDirectoryAsync(String, String, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName.

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, CancellationToken)

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level, and optionally including the base directory.

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, Encoding, CancellationToken)

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level and character encoding for entry names, and optionally including the base directory.

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, Encoding, CancellationToken)

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

CreateFromDirectoryAsync(String, Stream, CancellationToken)

Source:
ZipFile.Create.Async.cs

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

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * System.IO.Stream * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destination As Stream, Optional cancellationToken As CancellationToken = Nothing) As Task

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.

cancellationToken
CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

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.

An asynchronous operation is cancelled.

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

CreateFromDirectoryAsync(String, String, CancellationToken)

Source:
ZipFile.Create.Async.cs

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName.

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * string * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destinationArchiveFileName As String, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

sourceDirectoryName
String

The path to the directory on the file system to be archived.

destinationArchiveFileName
String

The name of the archive to be created.

cancellationToken
CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Exceptions

sourceDirectoryName or destinationArchiveFileName is a zero-length string, contains only whitespace, or contains one or more invalid characters as defined by InvalidPathChars.

sourceDirectoryName or destinationArchiveFileName is null.

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

For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

The path specified in sourceDirectoryName or destinationArchiveFileName is invalid, (for example, it is on an unmapped drive).

-or-

The directory specified by sourceDirectoryName does not exist.

destinationArchiveFileName already exists.

-or-

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

destinationArchiveFileName specified a directory.

-or-

The caller does not have the required permission.

sourceDirectoryName or destinationArchiveFileName is in an invalid format.

An asynchronous operation is cancelled.

Remarks

The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive is created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method does not include the base directory into the archive.

If an error is encountered while adding files to the archive, this method stops adding files and leaves the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.If an entry with the specified name already exists in the archive, a second entry is created that has an identical name. Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm is used; the ZipArchive will not impose its own default.

(Currently, the underlying compression algorithm is provided by the DeflateStream class.)

Applies to

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, CancellationToken)

Source:
ZipFile.Create.Async.cs

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level, and optionally including the base directory.

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * System.IO.Stream * System.IO.Compression.CompressionLevel * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destination As Stream, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task

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.

cancellationToken
CancellationToken

The token to monitor for cancellation requests.

Returns

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.

An asynchronous operation is cancelled.

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

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, CancellationToken)

Source:
ZipFile.Create.Async.cs

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * string * System.IO.Compression.CompressionLevel * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

sourceDirectoryName
String

The path to the directory on the file system to be archived.

destinationArchiveFileName
String

The name of the archive to be created.

compressionLevel
CompressionLevel

One of the enumeration values that specifies the level of the compression (speed/memory vs. compressed size trade-off).

includeBaseDirectory
Boolean

true to indicate that a directory named sourceDirectoryName should be included at the root of the archive. false to indicate that the files and directories in sourceDirectoryName should be included directly in the archive.

cancellationToken
CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Exceptions

sourceDirectoryName or destinationArchiveFileName is a zero-length string, contains only whitespace, or contains one or more invalid characters as defined by InvalidPathChars.

sourceDirectoryName or destinationArchiveFileName is null.

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

For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

The path specified in sourceDirectoryName or destinationArchiveFileName is invalid, (for example, it is on an unmapped drive).

-or-

The directory specified by sourceDirectoryName does not exist.

destinationArchiveFileName already exists.

-or-

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

destinationArchiveFileName specified a directory.

-or-

The caller does not have the required permission.

sourceDirectoryName or destinationArchiveFileName is in an invalid format.

An asynchronous operation is cancelled.

Remarks

The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created.

If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive.

If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name. Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default.

(Currently, the underlying compression algorithm is provided by the DeflateStream class.)

Applies to

CreateFromDirectoryAsync(String, Stream, CompressionLevel, Boolean, Encoding, CancellationToken)

Source:
ZipFile.Create.Async.cs

Asynchronously creates a zip archive in the specified stream that contains the files and directories from the specified directory, using the specified compression level and character encoding for entry names, and optionally including the base directory.

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Text.Encoding? entryNameEncoding, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * System.IO.Stream * System.IO.Compression.CompressionLevel * bool * System.Text.Encoding * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destination As Stream, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, entryNameEncoding As Encoding, Optional cancellationToken As CancellationToken = Nothing) As Task

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

cancellationToken
CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

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.

An asynchronous operation is cancelled.

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

CreateFromDirectoryAsync(String, String, CompressionLevel, Boolean, Encoding, CancellationToken)

Source:
ZipFile.Create.Async.cs

Asynchronously creates a zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName.

public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationArchiveFileName, System.IO.Compression.CompressionLevel compressionLevel, bool includeBaseDirectory, System.Text.Encoding? entryNameEncoding, System.Threading.CancellationToken cancellationToken = default);
static member CreateFromDirectoryAsync : string * string * System.IO.Compression.CompressionLevel * bool * System.Text.Encoding * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function CreateFromDirectoryAsync (sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As CompressionLevel, includeBaseDirectory As Boolean, entryNameEncoding As Encoding, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

sourceDirectoryName
String

The path to the directory on the file system to be archived.

destinationArchiveFileName
String

The name of the archive to be created.

compressionLevel
CompressionLevel

One of the enumeration values that specifies the level of the compression (speed/memory vs. compressed size trade-off).

includeBaseDirectory
Boolean

true to indicate that a directory named sourceDirectoryName should be included at the root of the archive. false to indicate that the files and directories in sourceDirectoryName should be included directly in the archive.

entryNameEncoding
Encoding

The encoding to use when reading or writing entry names and comments in this ZipArchive.

cancellationToken
CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Exceptions

sourceDirectoryName or destinationArchiveFileName is a zero-length string, contains only whitespace, or contains one or more invalid characters as defined by InvalidPathChars.

sourceDirectoryName or destinationArchiveFileName is null.

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

For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

The path specified in sourceDirectoryName or destinationArchiveFileName is invalid, (for example, it is on an unmapped drive).

-or-

The directory specified by sourceDirectoryName does not exist.

destinationArchiveFileName already exists.

-or-

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

destinationArchiveFileName specified a directory.

-or-

The caller does not have the required permission.

sourceDirectoryName or destinationArchiveFileName is in an invalid format.

An asynchronous operation is cancelled.

Remarks

The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created.

If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive.

If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name. Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default.

(Currently, the underlying compression algorithm is provided by the DeflateStream class.)

Specifying a value for entryNameEncoding other than null is discouraged.

However, this may be necessary for interoperability with zip archive tools and libraries that do not correctly support UTF-8 encoding for entry names or comments.

  • If entryNameEncoding is not specified (== null):
    • For file names or comments that contain characters outside the ASCII range:
      The language encoding flag (EFS) will be set in the general purpose bit flag of the local file header of the corresponding entry, and UTF-8 (Encoding.UTF8) will be used to encode the entry name and comment into bytes.
    • For file names or comments that do not contain characters outside the ASCII range:
      The language encoding flag (EFS) will not be set in the general purpose bit flag of the local file header of the corresponding entry, and the current system default code page (Encoding.Default) will be used to encode the entry names and comments into bytes.
  • If entryNameEncoding is specified (!= null):
    • The specified entryNameEncoding will always be used to encode the entry names and comments into bytes.
      The language encoding flag (EFS) in the general purpose bit flag of the local file header for each entry will be set if and only if the specified entryNameEncoding is a UTF-8 encoding.

Unicode encodings other than UTF-8 can't be used for the entryNameEncoding, otherwise an ArgumentException is thrown.

Applies to