Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

August 31, 2007

C# - Write Zip with Progress Monitoring and Abort Capability

This example demonstrates how to create a large zip which can be aborted at any point during the zipping process. The user interface is kept responsive by calling Application.DoEvents from within the event callbacks. AbortCheck callbacks are used because if only PercentDone callbacks are used, too much time may elapse between callbacks (because PercentDone is only called when the percentage completion increases). Also, notice that when zipping begins, the example program gives focus to the Cancel button. If you don’t set the focus, the 1st click on the Cancel button does nothing more than set focus to the button and then the 2nd click causes the cancel to happen. By programmatically setting the focus, the zip is cancelled on the 1st click of the Cancel button.

The sample program user-interface looks like this:
C# Zip Progress Example Program
Here’s the source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
	
namespace ZipWithEvents
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
	
        private bool bCancelled = false;
	
        private void zipButton_Click(object sender, EventArgs e)
        {
            bCancelled = false;
	
            Chilkat.Zip zip = new Chilkat.Zip();
            bool unlocked = zip.UnlockComponent("anything");
	
            if (!unlocked)
            {
                MessageBox.Show(zip.LastErrorText);
                return;
            }
	
            zip.NewZip("test.zip");
            zip.EnableEvents = true;
            zip.OnPercentDone += new Chilkat.Zip.PercentDoneEventHandler(OnPercentDone);
            zip.OnAbortCheck += new Chilkat.Zip.AbortCheckEventHandler(OnAbortCheck);
            zip.OnFileAdded += new Chilkat.Zip.FileAddedEventHandler(OnFileAdded);
	
            // When traversing an entire directory tree, this may consume a significant amount
            // of time.  Therefore, use the FileAdded event to get a callback after each
            // file is added, allowing us to abort at any point.
            // Note: AppendFiles only addes references to files to the zip object.
            // It does not read the contents of the files.  Files are streamed into
            // the .zip when WriteZip, WriteZipAndClose, or WriteExe are called.
            zip.AppendFiles("c:/temp/*", true);
	
            // Give focus to the cancel button...
            cancelButton.Focus();
	
            bool success = zip.WriteZipAndClose();
            if (!success)
            {
                MessageBox.Show(zip.LastErrorText);
            }
	
            else
            {
                MessageBox.Show("Success");
            }
	
            return;
        }
	
        public void OnFileAdded(object source, Chilkat.FileAddedEventArgs args)
        {
            if (bCancelled)
            {
                args.Abort = true;
            }
            Application.DoEvents();
        }
	
        public void OnAbortCheck(object source, Chilkat.AbortCheckEventArgs args)
        {
            if (bCancelled)
            {
                args.Abort = true;
            }
            Application.DoEvents();
        }
	
        public void OnPercentDone(object source, Chilkat.PercentDoneEventArgs args)
        {
            progressBar1.Value = args.PercentDone;
	
            if (bCancelled)
            {
                args.Abort = true;
            }
            Application.DoEvents();
	
        }
	
        private void cancelButton_Click(object sender, EventArgs e)
        {
            bCancelled = true;
        }
	
    }
}


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.