Muokkaa

Jaa


ZipFile.ExtractToDirectory Method

Definition

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

Overloads

ExtractToDirectory(String, String, Encoding, Boolean)

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

ExtractToDirectory(Stream, String, Encoding, Boolean)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, uses the specified character encoding for entry names and comments, and optionally allows choosing if the files in the destination directory should be overwritten.

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

ExtractToDirectory(String, String, Boolean)

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

ExtractToDirectory(Stream, String, Boolean)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, and optionally allows choosing if the files in the destination directory should be overwritten.

ExtractToDirectory(String, String)

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

ExtractToDirectory(Stream, String)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system.

ExtractToDirectory(Stream, String, Encoding)

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system and uses the specified character encoding for entry names and comments.

ExtractToDirectory(String, String, Encoding, Boolean)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

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

public:
 static void ExtractToDirectory(System::String ^ sourceArchiveFileName, System::String ^ destinationDirectoryName, System::Text::Encoding ^ entryNameEncoding, bool overwriteFiles);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding, bool overwriteFiles);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding entryNameEncoding, bool overwriteFiles);
static member ExtractToDirectory : string * string * System.Text.Encoding * bool -> unit
Public Shared Sub ExtractToDirectory (sourceArchiveFileName As String, destinationDirectoryName As String, entryNameEncoding As Encoding, overwriteFiles As Boolean)

Parameters

sourceArchiveFileName
String

The path on the file system to the archive that is to be extracted.

destinationDirectoryName
String

The path to the destination directory on the file system.

entryNameEncoding
Encoding

The encoding to use when reading entry names and comments in this ZipArchive.

overwriteFiles
Boolean

true to overwrite files; false otherwise.

Exceptions

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

-or-

entryNameEncoding is set to a Unicode encoding other than UTF-8.

sourceArchiveFileName or destinationDirectoryName is null.

sourceArchiveFileName or destinationDirectoryName specifies a path, a file name, or both that exceed the system-defined maximum length.

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

overwriteFiles is false and an archive entry to extract has the same name as a file that already exists in destinationDirectoryName.

-or-

An I/O error has occurred.

-or-

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 result in a file destination that is outside the destination directory (for example, because of 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.

sourceArchiveFileName or destinationDirectoryName is in an invalid format.

sourceArchiveFileName was not found.

The archive specified by sourceArchiveFileName is not a valid ZipArchive.

-or-

An archive entry was not found or was corrupt.

-or-

An archive entry has been compressed using a compression method that is not supported.

Remarks

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

Each entry will be extracted such that the extracted file has the same relative path to the destinationDirectoryName as the entry has to the archive.

The sourceArchiveFileName and destinationDirectoryName parameters accept both relative and absolute paths. A relative path is interpreted as relative to the current working directory.

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) will be used.

Applies to

ExtractToDirectory(Stream, String, Encoding, Boolean)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, uses the specified character encoding for entry names and comments, and optionally allows choosing if the files in the destination directory should be overwritten.

public:
 static void ExtractToDirectory(System::IO::Stream ^ source, System::String ^ destinationDirectoryName, System::Text::Encoding ^ entryNameEncoding, bool overwriteFiles);
public static void ExtractToDirectory (System.IO.Stream source, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding, bool overwriteFiles);
static member ExtractToDirectory : System.IO.Stream * string * System.Text.Encoding * bool -> unit
Public Shared Sub ExtractToDirectory (source As Stream, destinationDirectoryName As String, entryNameEncoding As Encoding, overwriteFiles As Boolean)

Parameters

source
Stream

The stream from which the zip archive 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 and comments 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 or comments.

overwriteFiles
Boolean

true to overwrite files; false otherwise.

Exceptions

destinationDirectoryName> 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 source is null.

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

overwriteFiles is false and an archive entry to extract has the same name as an entry that has already been extracted or that exists in destinationDirectoryName.

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

destinationDirectoryName contains an invalid format.

The archive contained in the source stream 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 the files in the zip archive contained in source 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 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) will be used.

Applies to

ExtractToDirectory(String, String, Encoding)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

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

public:
 static void ExtractToDirectory(System::String ^ sourceArchiveFileName, System::String ^ destinationDirectoryName, System::Text::Encoding ^ entryNameEncoding);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding entryNameEncoding);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding);
static member ExtractToDirectory : string * string * System.Text.Encoding -> unit
Public Shared Sub ExtractToDirectory (sourceArchiveFileName As String, destinationDirectoryName As String, entryNameEncoding As Encoding)

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 and comments 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 or comments.

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.

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 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 or that exists in destinationDirectoryName.

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, if necessary. 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 and comments 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, the entry names and comments are decoded by using the specified encoding.
  • For entries where the language encoding flag is set, the entry names and comments are decoded by using UTF-8.

If entryNameEncoding is set to null, entry names and comments 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 and comments are decoded by using the current system default code page.
  • For entries where the language encoding flag is set, the entry names and comments are decoded by using UTF-8.

Applies to

ExtractToDirectory(String, String, Boolean)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

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

public:
 static void ExtractToDirectory(System::String ^ sourceArchiveFileName, System::String ^ destinationDirectoryName, bool overwriteFiles);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName, bool overwriteFiles);
static member ExtractToDirectory : string * string * bool -> unit
Public Shared Sub ExtractToDirectory (sourceArchiveFileName As String, destinationDirectoryName As String, overwriteFiles As Boolean)

Parameters

sourceArchiveFileName
String

The path on the file system to the archive that is to be extracted.

destinationDirectoryName
String

The path to the destination directory on the file system.

overwriteFiles
Boolean

true to overwrite files; false otherwise.

Exceptions

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

sourceArchiveFileName or destinationDirectoryName is null.

sourceArchiveFileName or destinationDirectoryName specifies a path, a file name, or both that exceed the system-defined maximum length.

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

overwriteFiles is false and destinationDirectoryName already contains a file with the same name as a file that's being extracted.

-or-

An I/O error has occurred.

-or-

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 result in a file destination that is outside the destination directory (for example, because of parent directory accessors).

-or-

A ZipArchiveEntry has the same name as an entry from the same archive that's already been extracted.

The caller does not have the required permission.

sourceArchiveFileName or destinationDirectoryName is in an invalid format.

sourceArchiveFileName was not found.

The archive specified by sourceArchiveFileName is not a valid ZipArchive.

-or-

A ZipArchiveEntry was not found or was corrupt.

-or-

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

Remarks

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

Each entry will be extracted such that the extracted file has the same relative path to the destinationDirectoryName as the entry has to the archive.

The sourceArchiveFileName and destinationDirectoryName parameters accept both relative and absolute paths. A relative path is interpreted as relative to the current working directory.

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) will be used.

Applies to

ExtractToDirectory(Stream, String, Boolean)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system, and optionally allows choosing if the files in the destination directory should be overwritten.

public:
 static void ExtractToDirectory(System::IO::Stream ^ source, System::String ^ destinationDirectoryName, bool overwriteFiles);
public static void ExtractToDirectory (System.IO.Stream source, string destinationDirectoryName, bool overwriteFiles);
static member ExtractToDirectory : System.IO.Stream * string * bool -> unit
Public Shared Sub ExtractToDirectory (source As Stream, destinationDirectoryName As String, overwriteFiles As Boolean)

Parameters

source
Stream

The stream from which the zip archive 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.

overwriteFiles
Boolean

true to overwrite files; false otherwise.

Exceptions

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

destinationDirectoryName or source is null.

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

overwriteFiles is false and an archive entry to extract has the same name as an entry that has already been extracted or that exists in destinationDirectoryName.

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

destinationDirectoryName contains an invalid format.

The archive contained in the source stream 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 the files in the zip archive contained in source 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 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) will be used.

Applies to

ExtractToDirectory(String, String)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

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

public:
 static void ExtractToDirectory(System::String ^ sourceArchiveFileName, System::String ^ destinationDirectoryName);
public static void ExtractToDirectory (string sourceArchiveFileName, string destinationDirectoryName);
static member ExtractToDirectory : string * string -> unit
Public Shared Sub ExtractToDirectory (sourceArchiveFileName As String, destinationDirectoryName As String)

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.

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 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 or that exists in destinationDirectoryName.

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

class Program
{
    static void Main(string[] args)
    {
        string startPath = @".\start";
        string zipPath = @".\result.zip";
        string extractPath = @".\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}
open System.IO.Compression

let startPath = @".\start"
let zipPath = @".\result.zip"
let extractPath = @".\extract"

ZipFile.CreateFromDirectory(startPath, zipPath)

ZipFile.ExtractToDirectory(zipPath, extractPath)
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim startPath As String = ".\start"
        Dim zipPath As String = ".\result.zip"
        Dim extractPath As String = ".\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.

Applies to

ExtractToDirectory(Stream, String)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system.

public:
 static void ExtractToDirectory(System::IO::Stream ^ source, System::String ^ destinationDirectoryName);
public static void ExtractToDirectory (System.IO.Stream source, string destinationDirectoryName);
static member ExtractToDirectory : System.IO.Stream * string -> unit
Public Shared Sub ExtractToDirectory (source As Stream, destinationDirectoryName As String)

Parameters

source
Stream

The stream from which the zip archive 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> is Empty, contains only white space, or contains at least one invalid character.

destinationDirectoryName or source is null.

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

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

destinationDirectoryName contains an invalid format.

The archive contained in the source stream 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 the files in the zip archive contained in source 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 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) will be used.

Applies to

ExtractToDirectory(Stream, String, Encoding)

Source:
ZipFile.Extract.cs
Source:
ZipFile.Extract.cs

Extracts all the files from the zip archive stored in the specified stream and places them in the specified destination directory on the file system and uses the specified character encoding for entry names and comments.

public:
 static void ExtractToDirectory(System::IO::Stream ^ source, System::String ^ destinationDirectoryName, System::Text::Encoding ^ entryNameEncoding);
public static void ExtractToDirectory (System.IO.Stream source, string destinationDirectoryName, System.Text.Encoding? entryNameEncoding);
static member ExtractToDirectory : System.IO.Stream * string * System.Text.Encoding -> unit
Public Shared Sub ExtractToDirectory (source As Stream, destinationDirectoryName As String, entryNameEncoding As Encoding)

Parameters

source
Stream

The stream from which the zip archive 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 and comments 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 or comments.

Exceptions

destinationDirectoryName> 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 source is null.

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

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

destinationDirectoryName contains an invalid format.

The archive contained in the source stream 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 the files in the zip archive contained in source 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 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) will be used.

Applies to