Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

ZipFile.CreateFromDirectory Method (String, String)

.NET Framework 4.5

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

Namespace:  System.IO.Compression
Assembly:  System.IO.Compression.FileSystem (in System.IO.Compression.FileSystem.dll)
public static void CreateFromDirectory(
	string sourceDirectoryName,
	string destinationArchiveFileName
)

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.

ExceptionCondition
ArgumentException

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

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

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;
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);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5

Windows Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.