Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ZipFileExtensions.ExtractToDirectory Method

.NET Framework 4.5

Extracts all the files in the zip archive to a directory on the file system.

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

public static void ExtractToDirectory(
	this ZipArchive source,
	string destinationDirectoryName
)

Parameters

source
Type: System.IO.Compression.ZipArchive

The zip archive to extract files from.

destinationDirectoryName
Type: System.String

The path to the directory to place the extracted files in. You can specify either a relative or an absolute path. A relative path is interpreted as relative to the current working directory.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type ZipArchive. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
ArgumentException

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

ArgumentNullException

destinationDirectoryName is null.

PathTooLongException

The specified path exceeds 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

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

IOException

The directory specified by destinationDirectoryName already exists.

-or-

The name of an entry in the archive is Empty, contains only white space, or contains at least one invalid character.

-or-

Extracting an entry from the archive would create a file that is outside the directory specified by destinationDirectoryName. (For example, this might happen if the entry name contains parent directory accessors.)

-or-

Two or more entries in the archive have the same name.

UnauthorizedAccessException

The caller does not have the required permission to write to the destination directory.

NotSupportedException

destinationDirectoryName contains an invalid format.

InvalidDataException

An archive entry cannot be found or is corrupt.

-or-

An archive entry was compressed by using a compression method that is not supported.

This method creates the directory specified by destinationDirectoryName. If the destination directory already exists, this method does not overwrite it; it throws an IOException exception. The method also creates subdirectories that reflect the hierarchy in the zip archive. If an error occurs during extraction, the archive remains partially extracted. Each extracted file has the same relative path to the directory specified by destinationDirectoryName as its source entry has to the root of the archive.

The following example shows how to create a new entry in a zip archive from an existing file, and extract the archive to a new directory.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"c:\users\exampleuser\start.zip";
            string extractPath = @"c:\users\exampleuser\extract";
            string newFile = @"c:\users\exampleuser\NewFile.txt";

            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(newFile, "NewEntry.txt");
                archive.ExtractToDirectory(extractPath);
            } 
        }
    }
}

.NET Framework

Supported in: 4.5

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.

Show:
© 2014 Microsoft