Ανάγνωση στα Αγγλικά Επεξεργασία

Κοινή χρήση μέσω


ZipFileExtensions.ExtractToDirectory Method

Definition

Overloads

ExtractToDirectory(ZipArchive, String)

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

ExtractToDirectory(ZipArchive, String, Boolean)

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

ExtractToDirectory(ZipArchive, String)

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 static void ExtractToDirectory (this System.IO.Compression.ZipArchive source, string destinationDirectoryName);

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.

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

Remarks

This method creates the directory specified by destinationDirectoryName. 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.

Applies to

.NET 9 και άλλες εκδόσεις
Προϊόν Εκδόσεις
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ExtractToDirectory(ZipArchive, String, Boolean)

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 static void ExtractToDirectory (this System.IO.Compression.ZipArchive source, string destinationDirectoryName, bool overwriteFiles);

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.

overwriteFiles
Boolean

true to overwrite existing files; false otherwise.

Exceptions

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

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

destinationArchiveFileName 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

The specified directory may already exist. This method creates the specified directory and all subdirectories if necessary.

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

.NET 9 και άλλες εκδόσεις
Προϊόν Εκδόσεις
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1