Chilkat ActiveX Components

  Chilkat .NET Components

  Chilkat C++ Libraries

 

.NET Zip Compression Component C# Zip Compression Example Programs

Version 10.1.0

Powerful .NET Zip Compression Component
for Visual Basic .NET / C++ .NET / C# .NET / J# .NET (Java)

30-Day Unlock CodeDocumentation Direct DownloadLicense

C# Zip Compression Source Code Examples

Download the Projects and Source Code

  1. Create a Zip File and Demonstrate All Progress Monitoring Events (C# Source Code)
  2. Create a Password-Protected Zip File (C# Source Code)
  3. Create an AES Encrypted Zip File (C# Source Code)
  4. Open a Zip File and List the Contents (C# Source Code)
  5. Open and Unzip a Zip File (C# Source Code)

Create a Zip File and Demonstrate All Progress Monitoring Events

// This example will show how to handle each event when writing a Zip.
// You do not have to implement them, only the events you wish to receive.

// This event is called when WriteZip begins.
private void OnWriteZipBegin(object source, EventArgs args)
{
	listBox1.Items.Add("WriteZip begin...");
	createStatus.Text = "Writing Zip...";
	createStatus.Refresh();
}

// This event is called just before a file is to be written to the Zip.
// You can abort the Write by setting args.Abort to true.
private void OnToBeZipped(object source, ToBeZippedEventArgs args)
{
	//args.Abort = true;
	listBox1.Items.Add("Compressing " + args.FileName + "(" + args.FileSize + " bytes)");
	createStatus.Text = "Compressing " + args.FileName + "(" + args.FileSize + " bytes)";
	createStatus.Refresh();
}

// This event is called just after a file has been written to the Zip.
// You can abort the Write by setting args.Abort to true.
private void OnFileZipped(object source, FileZippedEventArgs args)
{
	//args.Abort = true;
	listBox1.Items.Add("Compressed " + args.FileName + "(" + args.FileSize + " bytes, compressed to " + 
		args.CompressedSize + " bytes)");
	createStatus.Text = "Compressed " + args.FileName + "(" + args.FileSize + " bytes, compressed to " + 
		args.CompressedSize + " bytes)";
	createStatus.Refresh();
}

// This event is called when WriteZip ends.
private void OnWriteZipEnd(object source, EventArgs args)
{
	listBox1.Items.Add("WriteZip finished.");
	createStatus.Text = "Write finished.";
	createStatus.Refresh();
}

private void OnAddFilesBegin(object source, EventArgs args)
{
	listBox1.Items.Add("AppendFiles begin...");
	createStatus.Text = "Appending Files...";
	createStatus.Refresh();
}

// This is an event called while the AppendFiles method is running, and is
// called just before a file is to be appended to the Zip object (in memory).
// You have a chance to prevent any file from being included by setting
// args.Exclude = true in the event handler.
private void OnToBeAdded(object source, ToBeAddedEventArgs args)
{
	//args.Exclude = true;
	listBox1.Items.Add("Appending " + args.FileName + "(" + args.FileSize + " bytes)");
	createStatus.Text = "Appending " + args.FileName + "(" + args.FileSize + " bytes)";
	createStatus.Refresh();
}

// This event is called while AppendFiles is running, just after the file is added to the Zip.
// You can Abort the AppendFiles method by setting args.Abort = true.
private void OnFileAdded(object source, FileAddedEventArgs args)
{
	//args.Abort = true;
	listBox1.Items.Add("Appended " + args.FileName + "(" + args.FileSize + " bytes)");
	createStatus.Text = "Appended " + args.FileName + "(" + args.FileSize + " bytes)";
	createStatus.Refresh();
}

private void OnAddFilesEnd(object source, EventArgs args)
{
	listBox1.Items.Add("AppendFiles finished.");
	createStatus.Text = "AppendFiles finished.";
	createStatus.Refresh();

}

// Handle the event for getting the progress while writing the Zip.
private void OnWriteZipPercentDone(object source, WriteZipPercentDoneEventArgs args)
{
	progressBar1.Value = args.PercentDone;
}

private void Zip_Click(object sender, System.EventArgs e)
{
	listBox1.Items.Clear();

	Zip zip = new Zip();
	zip.UnlockComponent(unlockCode);

   // Enable events.  (Events are disabled by default)
   zip.EnableEvents = true;

	// These are not required, but for the example we will show how to implement
	// each event offered by Chilkat Zip.NET
	zip.OnWriteZipBegin += new Zip.WriteZipBeginEventHandler(OnWriteZipBegin);
	zip.OnToBeZipped += new Zip.ToBeZippedEventHandler(OnToBeZipped);
	zip.OnFileZipped += new Zip.FileZippedEventHandler(OnFileZipped);
	zip.OnWriteZipPercentDone += new Zip.WriteZipPercentDoneEventHandler(OnWriteZipPercentDone);
	zip.OnWriteZipEnd += new Zip.WriteZipEndEventHandler(OnWriteZipEnd);

	zip.OnAddFilesBegin += new Zip.AddFilesBeginEventHandler(OnAddFilesBegin);
	zip.OnToBeAdded += new Zip.ToBeAddedEventHandler(OnToBeAdded);
	zip.OnFileAdded += new Zip.FileAddedEventHandler(OnFileAdded);
	zip.OnAddFilesEnd += new Zip.AddFilesEndEventHandler(OnAddFilesEnd);


	zip.NewZip(ZipFilename.Text);

	// Lets set some exclusions for files we don't want in the Zip.
	StringArray exclusions = new StringArray();
	exclusions.Append("*.dll");
	exclusions.Append("*.ncb");
	exclusions.Append("*.pdb");
	exclusions.Append("*.obj");
	zip.SetExclusions(exclusions);
	//createStatus.Text = "Zipping Files, please wait...";
	//createStatus.Refresh();

	// Add references to the external files to be included in the Zip.  The files are
	// not loaded into memory nor are they compressed at this point.
	if (!zip.AppendFiles(FilePattern.Text,true))
	{
		createStatus.Text = "Failed to append files, check errors.xml";
		zip.SaveLastError("errors.xml");
		return;
	}

	// Write the Zip and close it.  This is where the compression occurs.
	if (!zip.WriteZipAndClose())
	{
		createStatus.Text = "Failed to write Zip, check errors.xml";
		zip.SaveLastError("errors.xml");
		return;
	}
	//createStatus.Text = "Zip file created";

}

 

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

Components for Microsoft Windows XP, 2000, NT, 95/98/ME
Chilkat Self-Extracting EXE
XML Parser