Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ZipFile.CreateFromDirectory Method (String, String, CompressionLevel, Boolean, Encoding)

.NET Framework 4.6 and 4.5

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.

Namespace:  System.IO.Compression
Assembly:  System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)

public static void CreateFromDirectory(
	string sourceDirectoryName,
	string destinationArchiveFileName,
	CompressionLevel compressionLevel,
	bool includeBaseDirectory,
	Encoding entryNameEncoding
)

Parameters

sourceDirectoryName
Type: System.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
Type: System.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
Type: System.IO.Compression.CompressionLevel

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

includeBaseDirectory
Type: System.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
Type: System.Text.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.

ExceptionCondition
ArgumentException

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.

ArgumentNullException

sourceDirectoryName or destinationArchiveFileName is null.

PathTooLongException

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 not exceed 248 characters, and file names must not exceed 260 characters.

DirectoryNotFoundException

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

IOException

destinationArchiveFileName already exists.

-or-

A file in the specified directory could not be opened.

UnauthorizedAccessException

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.

NotSupportedException

sourceDirectoryName or destinationArchiveFileName contains an invalid format.

-or-

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.

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 are encoded by using the specified encoding. 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,

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

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

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

.NET Framework

Supported in: 4.6, 4.5
Show:
© 2015 Microsoft