ZipFile.ExtractToDirectory Method

Definition

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

Overloads

ExtractToDirectory(String, String)

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

ExtractToDirectory(String, String, Boolean)
ExtractToDirectory(String, String, Encoding)

Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names.

ExtractToDirectory(String, String, Encoding, Boolean)

ExtractToDirectory(String, String)

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

public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName);
Parameters
sourceArchiveFileName
String

The path to the archive that is to be extracted.

destinationDirectoryName
String

The path to the directory in which to place the extracted files, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

Exceptions

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

destinationDirectoryName or sourceArchiveFileName is null.

The specified path in destinationDirectoryName or sourceArchiveFileName 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.

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

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 archive entry 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-

An archive entry to extract has the same name as an entry that has already been extracted from the same archive.

The caller does not have the required permission to access the archive or the destination directory.

destinationDirectoryName or sourceArchiveFileName contains an invalid format.

sourceArchiveFileName was not found.

The archive specified by sourceArchiveFileName is not a valid zip archive.

-or-

An archive entry was not found or was corrupt.

-or-

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

Examples

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

        ZipFile.ExtractToDirectory(zipPath, extractPath)
    End Sub

End Module

Remarks

This method creates the specified directory and all subdirectories. The destination directory cannot already exist. Exceptions related to validating the paths in the destinationDirectoryName or sourceArchiveFileName parameters are thrown before extraction. Otherwise, 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.

ExtractToDirectory(String, String, Boolean)

public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, bool overwriteFiles);
Parameters
sourceArchiveFileName
String
destinationDirectoryName
String
overwriteFiles
Boolean

ExtractToDirectory(String, String, Encoding)

Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names.

public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding entryNameEncoding);
Parameters
sourceArchiveFileName
String

The path to the archive that is to be extracted.

destinationDirectoryName
String

The path to the directory in which to place the extracted files, specified as a relative or absolute path. A relative path is interpreted as relative to the current working 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.

Exceptions

destinationDirectoryName or sourceArchiveFileName 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.

destinationDirectoryName or sourceArchiveFileName is null.

The specified path in destinationDirectoryName or sourceArchiveFileName 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.

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

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 archive entry 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-

An archive entry to extract has the same name as an entry that has already been extracted from the same archive.

The caller does not have the required permission to access the archive or the destination directory.

destinationDirectoryName or sourceArchiveFileName contains an invalid format.

sourceArchiveFileName was not found.

The archive specified by sourceArchiveFileName is not a valid zip archive.

-or-

An archive entry was not found or was corrupt.

-or-

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

Remarks

This method creates the specified directory and all subdirectories. The destination directory cannot already exist. Exceptions related to validating the paths in the destinationDirectoryName or sourceArchiveFileName parameters are thrown before extraction. Otherwise, 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.

If entryNameEncoding is set to a value other than null, entry names are decoded according to the following rules:

  • For entry names where the language encoding flag (in the general-purpose bit flag of the local file header) is not set, the entry names are decoded by using the specified encoding.

  • For entries where the language encoding flag is set, the entry names are decoded by using UTF-8.

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

  • For entries where the language encoding flag (in the general-purpose bit flag of the local file header) is not set, entry names are decoded by using the current system default code page.

  • For entries where the language encoding flag is set, the entry names are decoded by using UTF-8.

ExtractToDirectory(String, String, Encoding, Boolean)

public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding entryNameEncoding, bool overwriteFiles);
Parameters
sourceArchiveFileName
String
destinationDirectoryName
String
entryNameEncoding
Encoding
overwriteFiles
Boolean

Applies to