.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

ZipItem.Extract(String, AllowFileOverwriteMode) Method

Extracts the current archive item as a file into the specified directory and enables you to define the behavior in case of a file name conflict.

Namespace: DevExpress.Compression

Assembly: DevExpress.Docs.v21.1.dll

Declaration

public void Extract(
    string directory,
    AllowFileOverwriteMode allowFileOverwrite
)

Parameters

Name Type Description
directory String

A string that is the path to the directory to which the files are extracted.

allowFileOverwrite 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. In this situation, the AllowFileOverwriteMode parameter specifies the default action.

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;
        }
See Also