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

November 28, 2007

Zip C++ Progress Monitoring Example

This blog post shows how to implement progress monitoring for zip and unzip operations in C++. Create a C++ class that derives from CkZipProgress and override one or more methods.

// To create a Zip progress callback object, derive from CkZipProgress
// and provide implementations for one or more callbacks.  
// This example shows implementations that print
// the callback information to stdout.
class MyZipProgress : public CkZipProgress
    {
    public:
	MyZipProgress(void) { }
	virtual ~MyZipProgress(void) { }
	
	// Called periodically to check to see if the Zip / Unzip should be aborted.
	void AbortCheck(bool *abort)
	    {
	    printf("AbortCheck!\n");
	    }
	    
	// Called during zip/unzip method calls.
	void PercentDone(int pctDone, bool *abort)
	    {
	    printf("%d pct completed.\n",pctDone);
	
	    // to abort, set *abort = true
	    }

	// Called during AppendFiles and AppendFilesEx
	void ToBeAdded(const char *fileName,
	    unsigned long fileSize,
	    bool *excludeFlag)
	    {
	    // To exclude a file from being added, set *excludeFlag = true
	
	    printf("To be added: %s, %d bytes\n",fileName,fileSize);
	    }
	
	// Called during AppendFiles and AppendFilesEx
	void DirToBeAdded(const char *fileName,
	    bool *excludeFlag)
	    {
	    // To exclude a directory from being added, set *excludeFlag = true
	
	    printf("Dir to be added: %s\n",fileName);
	    }
	
	// Called during AppendFiles and AppendFilesEx
	void FileAdded(const char *fileName,
	    unsigned long fileSize,
	    bool *abort)
	    {
	    printf("Added: %s, %d bytes\n",fileName,fileSize);
	    }
	
	// Called during zipping methods
	void ToBeZipped(const char *fileName,
	    unsigned long fileSize,
	    bool *abort)
	    {
	    printf("To be zipped: %s, %d bytes\n",fileName,fileSize);
	    }
	
	// Called during zipping methods
	void FileZipped(const char *fileName,
	    unsigned long fileSize,
	    unsigned long compressedSize,
	    bool *abort)
	    {
	    printf("Zipped: %s, %d/%d bytes\n",fileName,fileSize,compressedSize);
	    }
	
	// Called during unzip methods
	void ToBeUnzipped(const char *fileName,
	    unsigned long compressedSize,
	    unsigned long fileSize,
	    bool *abort)
	    {
	    printf("To be unzipped: %s, %d/%d bytes\n",fileName,fileSize,compressedSize);
	    }
	
	// Called during unzip methods
	void FileUnzipped(const char *fileName,
	    unsigned long compressedSize,
	    unsigned long fileSize,
	    bool *abort)
	    {
	    printf("Unzipped: %s, %d/%d bytes\n",fileName,fileSize,compressedSize);
	    }
	
	// Called at the start of AppendFiles / AppendFilesEx
	void AddFilesBegin(void)
	    {
	    printf("AddFilesBegin\n");
	    }
	// Called at the end of AppendFiles / AppendFilesEx
	void AddFilesEnd(void)
	    {
	    printf("AddFilesEnd\n");
	    }
	// Called when beginning to zip
	void WriteZipBegin(void)
	    {
	    printf("WriteZipBegin\n");
	    }
	// Called when finished zipping
	void WriteZipEnd(void)
	    {
	    printf("WriteZipEnd\n");
	    }
	// Called when beginning to unzip
	void UnzipBegin(void)
	    {
	    printf("UnzipBegin\n");
	    }
	// Called when finished unzipping
	void UnzipEnd(void)
	    {
	    printf("UnzipEnd\n");
	    }
	
    };
	
void TestZipWithProgress(void)
    {
    CkZip zip;
    bool success = zip.UnlockComponent("Anything for 30-day trial");
    if (!success)
	{
	printf("%s\n",zip.lastErrorText());
	return;
	}
	
    MyZipProgress progress;
	
    zip.NewZip("test.zip");
    success = zip.AppendFilesEx("c:/temp/abc123/*",true,false,false,false,false,&progress);
    if (!success)
	{
	printf("%s\n",zip.lastErrorText());
	return;
	}
    success = zip.WriteZipAndClose(&progress);
    if (!success)
	{
	printf("%s\n",zip.lastErrorText());
	return;
	}
	
    success = zip.OpenZip("test.zip");
    if (!success)
	{
	printf("%s\n",zip.lastErrorText());
	return;
	}
    int count = zip.Unzip("./unzipDir",&progress);
    if (count < 0)
	{
	printf("%s\n",zip.lastErrorText());
	return;
	}
	
    return;
    }
	

Sample output:

AddFilesBegin
To be added: c:\temp\abc123\bonaireFishing.html, 14344 bytes
Added: c:\temp\abc123\bonaireFishing.html, 14344 bytes
To be added: c:\temp\abc123\gopackaging.html, 42377 bytes
Added: c:\temp\abc123\gopackaging.html, 42377 bytes
To be added: c:\temp\abc123\HelloWorld123.txt, 6080 bytes
Added: c:\temp\abc123\HelloWorld123.txt, 6080 bytes
Dir to be added: c:\temp\abc123\a
Dir to be added: c:\temp\abc123\b
To be added: c:\temp\abc123\a\dudeA.gif, 6221 bytes
Added: c:\temp\abc123\a\dudeA.gif, 6221 bytes
To be added: c:\temp\abc123\a\hamlet.xml, 279658 bytes
Added: c:\temp\abc123\a\hamlet.xml, 279658 bytes
To be added: c:\temp\abc123\a\pigs.xml, 8463 bytes
Added: c:\temp\abc123\a\pigs.xml, 8463 bytes
To be added: c:\temp\abc123\a\Setup.exe, 24576 bytes
Added: c:\temp\abc123\a\Setup.exe, 24576 bytes
To be added: c:\temp\abc123\b\dude.gif, 6221 bytes
Added: c:\temp\abc123\b\dude.gif, 6221 bytes
To be added: c:\temp\abc123\b\dudeC.gif, 6221 bytes
Added: c:\temp\abc123\b\dudeC.gif, 6221 bytes
To be added: c:\temp\abc123\b\hello.txt, 13 bytes
Added: c:\temp\abc123\b\hello.txt, 13 bytes
AddFilesEnd
WriteZipBegin
To be zipped: bonaireFishing.html, 14344 bytes
WriteZip 3 pct completed.
Zipped: bonaireFishing.html, 14344/3658 bytes
To be zipped: gopackaging.html, 42377 bytes
WriteZip 14 pct completed.
Zipped: gopackaging.html, 42377/9971 bytes
To be zipped: HelloWorld123.txt, 6080 bytes
WriteZip 15 pct completed.
Zipped: HelloWorld123.txt, 6080/48 bytes
To be zipped: a, 0 bytes
To be zipped: b, 0 bytes
To be zipped: a\dudeA.gif, 6221 bytes
WriteZip 17 pct completed.
Zipped: a\dudeA.gif, 6221/5619 bytes
To be zipped: a\hamlet.xml, 279658 bytes
WriteZip 32 pct completed.
WriteZip 47 pct completed.
WriteZip 63 pct completed.
WriteZip 78 pct completed.
WriteZip 88 pct completed.
Zipped: a\hamlet.xml, 279658/79281 bytes
To be zipped: a\pigs.xml, 8463 bytes
WriteZip 90 pct completed.
Zipped: a\pigs.xml, 8463/2798 bytes
To be zipped: a\Setup.exe, 24576 bytes
WriteZip 96 pct completed.
Zipped: a\Setup.exe, 24576/8731 bytes
To be zipped: b\dude.gif, 6221 bytes
WriteZip 98 pct completed.
Zipped: b\dude.gif, 6221/5619 bytes
To be zipped: b\dudeC.gif, 6221 bytes
WriteZip 99 pct completed.
Zipped: b\dudeC.gif, 6221/5619 bytes
To be zipped: b\hello.txt, 13 bytes
WriteZip 100 pct completed.
Zipped: b\hello.txt, 13/15 bytes
WriteZipEnd
UnzipBegin
AbortCheck!
AbortCheck!
Unzip 3 pct completed.
Unzip 11 pct completed.
Unzip 14 pct completed.
Unzip 15 pct completed.
Unzip 17 pct completed.
Unzip 25 pct completed.
Unzip 34 pct completed.
Unzip 42 pct completed.
Unzip 50 pct completed.
Unzip 59 pct completed.
Unzip 67 pct completed.
Unzip 75 pct completed.
Unzip 84 pct completed.
Unzip 88 pct completed.
Unzip 90 pct completed.
Unzip 96 pct completed.
Unzip 98 pct completed.
Unzip 99 pct completed.
Unzip 100 pct completed.
UnzipEnd


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.