System.IO.Compression.ZipFileExtensions.ExtractToDirectory C# (CSharp) Méthode

ExtractToDirectory() public static méthode

Extracts all of the files in the archive to a directory on the file system. The specified directory may already exist. This method will create all subdirectories and the specified directory if necessary. 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 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 datetime representable in the Zip timestamp format (midnight on January 1, 1980) will be used.
destinationDirectoryName is a zero-length string, contains only white space, /// 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. /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path is invalid, (for example, it is on an unmapped drive). An archive entry?s name is zero-length, contains only white space, or contains one or more invalid /// characters as defined by InvalidPathChars. -or- Extracting an archive entry would have resulted in a destination /// file that is outside destinationDirectoryName (for example, if the entry name contains parent directory accessors). /// -or- An archive entry 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. An archive entry was not found or was corrupt. /// -or- An archive entry has been compressed using a compression method that is not supported.
public static ExtractToDirectory ( this source, string destinationDirectoryName ) : void
source this
destinationDirectoryName string The path to the directory on the file system. /// The directory specified must not exist. The path is permitted to specify relative or absolute path information. /// Relative path information is interpreted as relative to the current working directory.
Résultat void
        public static void ExtractToDirectory(this ZipArchive source, string destinationDirectoryName)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            if (destinationDirectoryName == null)
                throw new ArgumentNullException(nameof(destinationDirectoryName));

            Contract.EndContractBlock();

            // Rely on Directory.CreateDirectory for validation of destinationDirectoryName.

            // Note that this will give us a good DirectoryInfo even if destinationDirectoryName exists:
            DirectoryInfo di = Directory.CreateDirectory(destinationDirectoryName);
            string destinationDirectoryFullPath = di.FullName;

            foreach (ZipArchiveEntry entry in source.Entries)
            {
                string fileDestinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, entry.FullName));

                if (!fileDestinationPath.StartsWith(destinationDirectoryFullPath, PathInternal.StringComparison))
                    throw new IOException(SR.IO_ExtractingResultsInOutside);

                if (Path.GetFileName(fileDestinationPath).Length == 0)
                {
                    // If it is a directory:

                    if (entry.Length != 0)
                        throw new IOException(SR.IO_DirectoryNameWithData);

                    Directory.CreateDirectory(fileDestinationPath);
                }
                else
                {
                    // If it is a file:
                    // Create containing directory:
                    Directory.CreateDirectory(Path.GetDirectoryName(fileDestinationPath));
                    entry.ExtractToFile(fileDestinationPath, overwrite: false);
                }
            }
        }