ZipFile.CreateFromDirectory Method (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.
Assembly: System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)
'Declaration Public Shared Sub CreateFromDirectory ( _ sourceDirectoryName As String, _ destinationArchiveFileName As String, _ compressionLevel As CompressionLevel, _ includeBaseDirectory As Boolean _ )
- 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.
- 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.
- Type: System.IO.Compression.CompressionLevel
One of the enumeration values that indicates whether to emphasize speed or compression effectiveness when creating the entry.
- 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.
sourceDirectoryName or destinationArchiveFileName is Empty, contains only white space, or contains at least one invalid character.
sourceDirectoryName or destinationArchiveFileName is Nothing.
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.
sourceDirectoryName is invalid or does not exist (for example, it is on an unmapped drive).
destinationArchiveFileName already exists.
A file in the specified directory could not be opened.
destinationArchiveFileName specifies a directory.
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.
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 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.
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.
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
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.