Language

ZipFileExtensions.ExtractToDirectory Method

Definition

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

Overloads

Name Description
ExtractToDirectory(ZipArchive, String, ZipExtractionOptions)
ExtractToDirectory(ZipArchive, String, Boolean)

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

ExtractToDirectory(ZipArchive, String)

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

ExtractToDirectory(ZipArchive, String, ZipExtractionOptions)

Source:
ZipFileExtensions.ZipArchive.Extract.cs
public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToDirectory(System::IO::Compression::ZipArchive ^ source, System::String ^ destinationDirectoryName, System::IO::Compression::ZipExtractionOptions ^ options);
public static void ExtractToDirectory(this System.IO.Compression.ZipArchive source, string destinationDirectoryName, System.IO.Compression.ZipExtractionOptions options);
static member ExtractToDirectory : System.IO.Compression.ZipArchive * string * System.IO.Compression.ZipExtractionOptions -> unit
<Extension()>
Public Sub ExtractToDirectory (source As ZipArchive, destinationDirectoryName As String, options As ZipExtractionOptions)

Parameters

source
ZipArchive
destinationDirectoryName
String

The path to the directory in which to place the extracted files. The directory can already exist. Missing directories are created as needed when extracting entries.

Remarks

Warning

This method must not be used to extract untrusted archives. It doesn't enforce any size limits, entry count limits, or other policies needed for safe extraction of untrusted archives. For more information on safely extracting archives from untrusted sources, see Best practices for using zip and tar archives.

The directory specified by destinationDirectoryName can already exist. This method creates the destination directory, its missing parent directories, and subdirectories as needed when extracting entries. If the archive is empty, this method doesn't create the destination directory.

Each extracted file has the same relative path to destinationDirectoryName as its source entry has to the root of the archive.

The destinationDirectoryName parameter accepts relative and absolute paths. A relative path is interpreted as relative to the current working directory.

If an error occurs during extraction, the archive remains partially extracted.

Set OverwriteFiles to true to overwrite existing files. The default is false, which causes extraction to throw an IOException if a file to be extracted already exists.

Use Password to supply the password for encrypted entries.

EntryNameEncoding isn't used by this overload. Entry names and comments are decoded using the encoding specified when the source archive is opened.

Symbolic links in the archive aren't recreated as symbolic links. If destinationDirectoryName or any of its parent directories is a pre-existing junction or symbolic link, the link is followed and extraction writes to the final target folder.

Applies to

ExtractToDirectory(ZipArchive, String, Boolean)

Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs

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

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToDirectory(System::IO::Compression::ZipArchive ^ source, System::String ^ destinationDirectoryName, bool overwriteFiles);
public static void ExtractToDirectory(this System.IO.Compression.ZipArchive source, string destinationDirectoryName, bool overwriteFiles);
static member ExtractToDirectory : System.IO.Compression.ZipArchive * string * bool -> unit
<Extension()>
Public Sub ExtractToDirectory (source As ZipArchive, destinationDirectoryName As String, overwriteFiles As Boolean)

Parameters

source
ZipArchive

The ZipArchive to extract.

destinationDirectoryName
String

The path to the destination directory on the file system. The path can be relative or absolute. A relative path is interpreted as relative to the current working directory. The directory can already exist. Missing directories are created as needed when extracting entries.

overwriteFiles
Boolean

true to overwrite existing files; false otherwise.

Exceptions

destinationDirectoryName is a zero-length string, contains only whitespace, or contains one or more invalid characters as defined by InvalidPathChars.

destinationDirectoryName is null.

The specified path, file name, or both exceed the system-defined maximum length.

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

The name of a ZipArchiveEntry is zero-length, contains only whitespace, or contains one or more invalid characters as defined by InvalidPathChars.

-or-

Extracting a ZipArchiveEntry would have resulted in a destination file that is outside destinationDirectoryName (for example, if the entry name contains parent directory accessors).

-or-

A ZipArchiveEntry has the same name as an already extracted entry from the same archive.

The caller does not have the required permission.

destinationDirectoryName is in an invalid format.

A ZipArchiveEntry was not found or was corrupt.

-or-

A ZipArchiveEntry has been compressed using a compression method that is not supported.

Remarks

Warning

This method must not be used to extract untrusted archives. It doesn't enforce any size limits, entry count limits, or other policies needed for safe extraction of untrusted archives. For more information on safely extracting archives from untrusted sources, see Best practices for using zip and tar archives.

The directory specified by destinationDirectoryName can already exist. This method creates the destination directory, its missing parent directories, and subdirectories as needed when extracting entries. If the archive is empty, this method doesn't create the destination directory.

If there is an error while extracting the archive, the archive will remain partially extracted.

Each entry is extracted such that the extracted file has the same relative path to destinationDirectoryName as the entry has to the root of the archive.

If a file to be archived has an invalid last modified time, the first date and time representable in the Zip timestamp format (midnight on January 1, 1980) is used.

Applies to

ExtractToDirectory(ZipArchive, String)

Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs
Source:
ZipFileExtensions.ZipArchive.Extract.cs

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

public:
[System::Runtime::CompilerServices::Extension]
 static void ExtractToDirectory(System::IO::Compression::ZipArchive ^ source, System::String ^ destinationDirectoryName);
public static void ExtractToDirectory(this System.IO.Compression.ZipArchive source, string destinationDirectoryName);
static member ExtractToDirectory : System.IO.Compression.ZipArchive * string -> unit
<Extension()>
Public Sub ExtractToDirectory (source As ZipArchive, destinationDirectoryName As String)

Parameters

source
ZipArchive

The zip archive to extract files from.

destinationDirectoryName
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. The directory can already exist. Missing directories are created as needed when extracting entries.

Exceptions

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

destinationDirectoryName is null.

The specified path exceeds the system-defined maximum length.

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

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.

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

destinationDirectoryName contains an invalid format.

An archive entry cannot be found or is corrupt.

-or-

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

Examples

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);
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim zipPath As String = "c:\users\exampleuser\end.zip"
        Dim extractPath As String = "c:\users\exampleuser\extract"
        Dim newFile As String = "c:\users\exampleuser\NewFile.txt"

        Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
            archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
            archive.ExtractToDirectory(extractPath)
        End Using
    End Sub

End Module

Remarks

Warning

This method must not be used to extract untrusted archives. It doesn't enforce any size limits, entry count limits, or other policies needed for safe extraction of untrusted archives. For more information on safely extracting archives from untrusted sources, see Best practices for using zip and tar archives.

The directory specified by destinationDirectoryName can already exist. This method creates the destination directory, its missing parent directories, and subdirectories as needed when extracting entries. If the archive is empty, this method doesn't create the destination directory.

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.

Applies to