Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

ZipArchive.AllowFileOverwrite Event

Occurs when the item extracted from the archive tries to overwrite a file that already exists.

Namespace: DevExpress.Compression

Assembly: DevExpress.Docs.v20.2.dll

Declaration

public event AllowFileOverwriteEventHandler AllowFileOverwrite

Event Data

The AllowFileOverwrite event's data class is AllowFileOverwriteEventArgs. The following properties provide information specific to this event:

Property Description
Cancel Gets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
TargetFilePath Gets the path to the file to which the zip item will be unzipped.
ZipItem Obtains the zip item for which a file conflict is detected.

Remarks

By handling the AllowFileOverwrite event, you can decide whether a certain file should be replaced with an item extracted from the archive. This functionality can be useful for implementing backup synchronization schemes.

To leave the conflicting file intact and skip to the next zip item, set the Cancel property of the event arguments to true.

The following code illustrates how to implement the requirement that the conflicting target file should be overwritten only if the corresponding file in the archive has been accessed no later than an hour ago.

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

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the AllowFileOverwrite event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also