officefileapi-devexpress-dot-compression-dot-ziparchive-58fddfc3.md
Occurs when the item extracted from the archive tries to overwrite a file that already exists.
Namespace : DevExpress.Compression
Assembly : DevExpress.Docs.v25.2.dll
NuGet Package : DevExpress.Document.Processor
public event AllowFileOverwriteEventHandler AllowFileOverwrite
Public Event AllowFileOverwrite As AllowFileOverwriteEventHandler
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. |
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;
}
Imports DevExpress.Compression
Public Sub UnzipArchiveConflict()
Dim pathToZipArchive As String = "Documents\Example.zip"
Dim pathToExtract As String = "Documents\!Extracted"
Using archive As ZipArchive = ZipArchive.Read(pathToZipArchive)
archive.OptionsBehavior.AllowFileOverwrite = AllowFileOverwriteMode.Custom
AddHandler archive.AllowFileOverwrite, AddressOf archive_AllowFileOverwrite
For Each item As ZipItem In archive
item.Extract(pathToExtract)
Next item
End Using
End Sub
Private Sub archive_AllowFileOverwrite(ByVal sender As Object, ByVal e As AllowFileOverwriteEventArgs)
Dim fi As New FileInfo(e.TargetFilePath)
If e.ZipItem.LastWriteTime < fi.LastWriteTime Then
e.Cancel = True
End If
End Sub
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.
zip-compression-and-archive-api-examples/CS/CompressionLibraryExamples/ZipExamples.cs#L197
archive.OptionsBehavior.AllowFileOverwrite = AllowFileOverwriteMode.Custom;
archive.AllowFileOverwrite += archive_AllowFileOverwrite;
foreach (ZipItem item in archive) {
zip-compression-and-archive-api-examples/VB/CompressionLibraryExamples/ZipExamples.vb#L202
archive.OptionsBehavior.AllowFileOverwrite = AllowFileOverwriteMode.Custom
AddHandler archive.AllowFileOverwrite, AddressOf archive_AllowFileOverwrite
For Each item As ZipItem In archive
See Also