C# Class System.IO.Compression.ZipFile

ファイルを表示 Open project: dotnet/corefx Class Usage Examples

Public Methods

Method Description
CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName ) : void

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method does not include the base directory into the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel, bool includeBaseDirectory ) : void

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel, bool includeBaseDirectory, Encoding entryNameEncoding ) : void

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

ExtractToDirectory ( string sourceArchiveFileName, string destinationDirectoryName ) : void

Extracts all of the files in the specified archive to a directory on the file system. The specified directory must not exist. This method will create all subdirectories and the specified directory. 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 path is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. 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.

ExtractToDirectory ( string sourceArchiveFileName, string destinationDirectoryName, Encoding entryNameEncoding ) : void

Extracts all of the files in the specified archive to a directory on the file system. The specified directory must not exist. This method will create all subdirectories and the specified directory. 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 path is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. 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.

OpenRead ( string archiveFileName ) : ZipArchive

Opens a ZipArchive on the specified path for reading. The specified file is opened with FileMode.Open.

Private Methods

Method Description
DoCreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel, bool includeBaseDirectory, Encoding entryNameEncoding ) : void
EnsureCapacity ( char &buffer, int min ) : void
EntryFromPath ( string entry, int offset, int length, char &buffer, bool appendPathSeparator = false ) : string
IsDirEmpty ( DirectoryInfo possiblyEmptyDir ) : bool
Open ( string archiveFileName, ZipArchiveMode mode ) : ZipArchive
Open ( string archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding ) : ZipArchive

Method Details

CreateFromDirectory() public static method

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories from the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method does not include the base directory into the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

sourceDirectoryName or destinationArchiveFileName is a zero-length /// string, contains only white space, or contains one or more invalid characters as defined by /// InvalidPathChars. sourceDirectoryName or destinationArchiveFileName is null. In sourceDirectoryName or destinationArchiveFileName, 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 path specified in sourceDirectoryName or destinationArchiveFileName /// is invalid, (for example, it is on an unmapped drive). /// -OR- The directory specified by sourceDirectoryName does not exist. destinationArchiveFileName already exists. /// -OR- An I/O error occurred while opening a file to be archived. destinationArchiveFileName specified a directory. /// -OR- The caller does not have the required permission. sourceDirectoryName or destinationArchiveFileName is /// in an invalid format.
public static CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName ) : void
sourceDirectoryName string The path to the directory on the file system to be archived.
destinationArchiveFileName string The name of the archive to be created.
return void

CreateFromDirectory() public static method

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

sourceDirectoryName or destinationArchiveFileName is a zero-length /// string, contains only white space, or contains one or more invalid characters as defined by /// InvalidPathChars. sourceDirectoryName or destinationArchiveFileName is null. In sourceDirectoryName or destinationArchiveFileName, 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 path specified in sourceDirectoryName or /// destinationArchiveFileName is invalid, (for example, it is on an unmapped drive). /// -OR- The directory specified by sourceDirectoryName does not exist. destinationArchiveFileName already exists. /// -OR- An I/O error occurred while opening a file to be archived. destinationArchiveFileName specified a directory. /// -OR- The caller does not have the required permission. sourceDirectoryName or destinationArchiveFileName /// is in an invalid format.
public static CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel, bool includeBaseDirectory ) : void
sourceDirectoryName string The path to the directory on the file system to be archived.
destinationArchiveFileName string The name of the archive to be created.
compressionLevel CompressionLevel The level of the compression (speed/memory vs. compressed size trade-off).
includeBaseDirectory bool true to indicate that a directory named sourceDirectoryName should /// be included at the root of the archive. false to indicate that the files and directories in sourceDirectoryName /// should be included directly in the archive.
return void

CreateFromDirectory() public static method

Creates a Zip archive at the path destinationArchiveFileName that contains the files and directories in the directory specified by sourceDirectoryName. The directory structure is preserved in the archive, and a recursive search is done for files to be archived. The archive must not exist. If the directory is empty, an empty archive will be created. If a file in the directory cannot be added to the archive, the archive will be left incomplete and invalid and the method will throw an exception. This method optionally includes the base directory in the archive. If an error is encountered while adding files to the archive, this method will stop adding files and leave the archive in an invalid state. The paths are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. If a file in the archive has data in the last write time field that is not a valid Zip timestamp, an indicator value of 1980 January 1 at midnight will be used for the file's last modified time.

If an entry with the specified name already exists in the archive, a second entry will be created that has an identical name.

Since no CompressionLevel is specified, the default provided by the implementation of the underlying compression algorithm will be used; the ZipArchive will not impose its own default. (Currently, the underlying compression algorithm is provided by the System.IO.Compression.DeflateStream class.)

sourceDirectoryName or destinationArchiveFileName is a zero-length /// string, contains only white space, or contains one or more invalid characters as defined by /// InvalidPathChars. sourceDirectoryName or destinationArchiveFileName is null. In sourceDirectoryName or destinationArchiveFileName, 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 path specified in sourceDirectoryName or /// destinationArchiveFileName is invalid, (for example, it is on an unmapped drive). /// -OR- The directory specified by sourceDirectoryName does not exist. destinationArchiveFileName already exists. /// -OR- An I/O error occurred while opening a file to be archived. destinationArchiveFileName specified a directory. /// -OR- The caller does not have the required permission. sourceDirectoryName or destinationArchiveFileName /// is in an invalid format.
public static CreateFromDirectory ( string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel, bool includeBaseDirectory, Encoding entryNameEncoding ) : void
sourceDirectoryName string The path to the directory on the file system to be archived.
destinationArchiveFileName string The name of the archive to be created.
compressionLevel CompressionLevel The level of the compression (speed/memory vs. compressed size trade-off).
includeBaseDirectory bool true to indicate that a directory named sourceDirectoryName should /// be included at the root of the archive. false to indicate that the files and directories in sourceDirectoryName /// should be included directly in the archive.
entryNameEncoding System.Text.Encoding The encoding to use when reading or writing entry names in this ZipArchive. /// /// NOTE: Specifying this parameter to values other than null is discouraged. /// However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support /// UTF-8 encoding for entry names.
/// This value is used as follows while creating the archive:
/// If entryNameEncoding is not specified (== null): /// /// For file names that contain characters outside the ASCII range:
/// The language encoding flag (EFS) will be set in the general purpose bit flag of the local file header of the corresponding entry, /// and UTF-8 (Encoding.UTF8) will be used in order to encode the entry name into bytes.
/// For file names that do not contain characters outside the ASCII range:
/// the language encoding flag (EFS) will not be set in the general purpose bit flag of the local file header of the corresponding entry, /// and the current system default code page (Encoding.Default) will be used to encode the entry names into bytes.
///
/// If entryNameEncoding is specified (!= null): /// /// The specified entryNameEncoding will always be used to encode the entry names into bytes. /// The language encoding flag (EFS) in the general purpose bit flag of the local file header for each entry will be set if and only /// if the specified entryNameEncoding is a UTF-8 encoding. /// /// Note that Unicode encodings other than UTF-8 may not be currently used for the entryNameEncoding, /// otherwise an is thrown. ///
return void

ExtractToDirectory() public static method

Extracts all of the files in the specified archive to a directory on the file system. The specified directory must not exist. This method will create all subdirectories and the specified directory. 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 path is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. 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.
sourceArchive or destinationDirectoryName is a zero-length string, contains only white space, /// or contains one or more invalid characters as defined by InvalidPathChars. sourceArchive or destinationDirectoryName is null. sourceArchive or destinationDirectoryName specifies a 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 path specified by sourceArchive or destinationDirectoryName is invalid, /// (for example, it is on an unmapped drive). The directory specified by destinationDirectoryName already exists. /// -or- An I/O error has occurred. -or- 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 result in a file destination that is outside the destination directory (for example, because of 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. sourceArchive or destinationDirectoryName is in an invalid format. sourceArchive was not found. The archive specified by sourceArchive: 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.
public static ExtractToDirectory ( string sourceArchiveFileName, string destinationDirectoryName ) : void
sourceArchiveFileName string The path to the archive on the file system that is to be extracted.
destinationDirectoryName string The path to the directory on the file system. The directory specified must not exist, but the directory that it is contained in must exist.
return void

ExtractToDirectory() public static method

Extracts all of the files in the specified archive to a directory on the file system. The specified directory must not exist. This method will create all subdirectories and the specified directory. 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 path is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. 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.
sourceArchive or destinationDirectoryName is a zero-length string, contains only white space, /// or contains one or more invalid characters as defined by InvalidPathChars. sourceArchive or destinationDirectoryName is null. sourceArchive or destinationDirectoryName specifies a 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 path specified by sourceArchive or destinationDirectoryName is invalid, /// (for example, it is on an unmapped drive). The directory specified by destinationDirectoryName already exists. /// -or- An I/O error has occurred. -or- 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 result in a file destination that is outside the destination directory (for example, because of 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. sourceArchive or destinationDirectoryName is in an invalid format. sourceArchive was not found. The archive specified by sourceArchive: 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.
public static ExtractToDirectory ( string sourceArchiveFileName, string destinationDirectoryName, Encoding entryNameEncoding ) : void
sourceArchiveFileName string The path to the archive on the file system that is to be extracted.
destinationDirectoryName string The path to the directory on the file system. The directory specified must not exist, but the directory that it is contained in must exist.
entryNameEncoding System.Text.Encoding The encoding to use when reading or writing entry names in this ZipArchive. /// /// NOTE: Specifying this parameter to values other than null is discouraged. /// However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support /// UTF-8 encoding for entry names.
/// This value is used as follows:
/// If entryNameEncoding is not specified (== null): /// /// For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is not set, /// use the current system default code page (Encoding.Default) in order to decode the entry name. /// For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is set, /// use UTF-8 (Encoding.UTF8) in order to decode the entry name. /// /// If entryNameEncoding is specified (!= null): /// /// For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is not set, /// use the specified entryNameEncoding in order to decode the entry name. /// For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is set, /// use UTF-8 (Encoding.UTF8) in order to decode the entry name. /// /// Note that Unicode encodings other than UTF-8 may not be currently used for the entryNameEncoding, /// otherwise an is thrown. ///
return void

OpenRead() public static method

Opens a ZipArchive on the specified path for reading. The specified file is opened with FileMode.Open.
archiveFileName is a zero-length string, contains only white space, or contains one /// or more invalid characters as defined by InvalidPathChars. archiveFileName is null. The specified archiveFileName exceeds 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 archiveFileName is invalid, (for example, it is on an unmapped drive). An unspecified I/O error occurred while opening the file. archiveFileName specified a directory. /// -OR- The caller does not have the required permission. The file specified in archiveFileName was not found. archiveFileName is in an invalid format. The specified file could not be interpreted as a Zip file.
public static OpenRead ( string archiveFileName ) : ZipArchive
archiveFileName string A string specifying the path on the filesystem to open the archive on. The path is permitted /// to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
return ZipArchive