Back to Devexpress

How to: Cancel Archivation

officefileapi-15242-zip-compression-and-archive-api-examples-how-to-cancel-archiving.md

latest2.5 KB
Original Source

How to: Cancel Archivation

  • Sep 19, 2023

To provide the capability to cancel an archiving process after it has been started, do the following:

  1. Create a ZipArchive class instance.
  2. Subscribe to the ZipArchive.Progress event.
  3. Set the CanContinueEventArgs.CanContinue property of event arguments to false , in case you decide to stop the operation.
  4. Call the ZipArchive.AddFile method for each selected file.
  5. Call the ZipArchive.Save method to create an archive and save it to a specified location.

View Example

csharp
using DevExpress.Compression;
        volatile bool stopProgress = false;

        public void CancelArchiveProgress() {
            string[] sourcefiles = this.sourceFiles;
            using (ZipArchive archive = new ZipArchive()) {
                archive.Progress += archive_Progress;
                foreach (string file in sourceFiles) {
                    archive.AddFile(file, "/");
                }
                archive.Save("Documents\\CancelArchiveProgress.zip");
            }
        }

        private void archive_Progress(object sender, ProgressEventArgs args) {
            args.CanContinue = !this.stopProgress;
        }
vb
Imports DevExpress.Compression
'INSTANT VB TODO TASK: There is no VB.NET equivalent to 'volatile':
'ORIGINAL LINE: volatile bool stopProgress = False;
        Private stopProgress As Boolean = False

        Public Sub CancelArchiveProgress()
            Dim sourcefiles() As String = Me.sourceFiles
            Using archive As New ZipArchive()
                AddHandler archive.Progress, AddressOf archive_Progress
                For Each file As String In Me.sourceFiles
                    archive.AddFile(file, "/")
                Next file
                archive.Save("Documents\CancelArchiveProgress.zip")
            End Using
        End Sub

        Private Sub archive_Progress(ByVal sender As Object, ByVal args As ProgressEventArgs)
            args.CanContinue = Not Me.stopProgress
        End Sub