ZipArchive.Extract(String, AllowFileOverwriteMode) Method
Extracts all archive items as files into the specified directory and enables you to define the behavior in case of file name conflicts.
Namespace: DevExpress.Compression
Assembly: DevExpress.Docs.v24.1.dll
NuGet Package: DevExpress.Document.Processor
Declaration
Parameters
Name | Type | Description |
---|---|---|
path | String | A string that is the path to the directory to which the files are extracted. |
mode | AllowFileOverwriteMode | An AllowFileOverwriteMode enumeration member that specifies the behavior if a file name conflict occurs. |
Remarks
If the archive contains directories, the directory structure will be recreated in the specified directory. The ZipItem.Name property value becomes the file name of the extracted file. If a file with the same name already exists, a file name conflict occurs. The AllowFileOverwriteMode parameter specifies the default action in this situation.
This example illustrates how to handle a file name conflict when files are extracted from archive. If a file with the same name exists, the ZipArchive.AllowFileOverwrite event occurs. You can handle this event and determine that if the file in the folder is newer than the zip item, the file in the folder should not be overwritten.
using DevExpress.Compression;
public void UnzipArchiveConflict() {
string pathToZipArchive = "Documents\\Example.zip";
string pathToExtract = "Documents\\!Extracted";
using (ZipArchive archive = ZipArchive.Read(pathToZipArchive)) {
archive.OptionsBehavior.AllowFileOverwrite = AllowFileOverwriteMode.Custom;
archive.AllowFileOverwrite += archive_AllowFileOverwrite;
foreach (ZipItem item in archive) {
item.Extract(pathToExtract);
}
}
}
private void archive_AllowFileOverwrite(object sender, AllowFileOverwriteEventArgs e) {
FileInfo fi = new FileInfo(e.TargetFilePath);
if (e.ZipItem.LastWriteTime < fi.LastWriteTime) e.Cancel = true;
}