Getting Started

This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.

August 2001

Add Some Zip to Your Apps

Here's an easy-to-use ActiveX DLL that adds zip capabilities to your apps.

by Stan Schultes

Reprinted with permission from Visual Basic Programmer's Journal, August 2001, Volume 11, Issue 8, Copyright 2001, Fawcette Technical Publications, Palo Alto, CA, USA. To subscribe, call 1-800-848-5523, 650-833-7100, visit www.vbpj.com, or visit The Development Exchange.

You can find many uses for a compression utility in your everyday work: archiving project files, shrinking files for e-mail, and using it for backups or a simple version control system. Many useful zip utilities abound on the Web, but you probably won't find a good free one you can embed into your Visual Basic apps without restriction.

The Info-ZIP group is an open-source project that forms the core engine of many zip utilities, including WinZip. It offers two dynamic link libraries (DLLs)—one for zipping and one for unzipping. The Info-ZIP DLLs are free to use and distribute (be sure you read their license terms), they're cross-platform, and they're quite functional. The downside: The DLLs for Windows are C DLLs, so they're not VB-friendly.

You can make the Info-ZIP DLLs VB-friendly by creating a reusable ActiveX wrapper DLL around them. I built such a wrapper, named ZipUp.dll, around Info-ZIP's zipping library (which I've renamed ZipUp23.dll; see the readme.txt file in this article's sample code). You can find ZipUp.dll in the sample code, as well as a test application, ZipUpTest, that shows you how the DLL works. ZipUp.dll uses some advanced VB programming techniques, so I'll summarize it only briefly rather than delve into how to create it. Instead, I'll concentrate on the sample application and how you can use ZipUp.dll in your apps. You can study the ZipUp.dll code and comments later to see how it works so you can use the same techniques to build yourself an unzip DLL.

ZipUp.dll is an ActiveX DLL built from a single class module (CZipUp) and a single standard module (modZip). In an ActiveX DLL project, you normally put all your code into the project's class module(s). But ZipUp needs code in its standard module because it contains callback functions, which must reside in a standard module. Info-ZIP's ZipUp23.dll uses callback functions to communicate information back to the calling application, hence the name. The callback functions reside in the modZip module and call CZipUp class methods to handle the actual zipping work.

The underlying ZipUp23.dll provides four callback functions, and ZipUp.dll implements three of them. ZipPrint and ZipService communicate progress and status from the zip operation back to the calling application, and ZipComment lets you prompt the user to add a comment to the zip file. The fourth underlying callback prompts for a password, but I didn't include the optional encryption components, so I didn't implement the callback. ZipUp.dll exposes two events: the Message event, which is fired whenever the underlying ZipUp23.dll sends a status message, and the FileComplete event, which is fired when the zip operation for each file finishes.

The ZipUp component creates a circular reference internally (a reference to itself) so the module-based callback functions can find the corresponding class method calls. The Reset class method breaks the circular reference so you can destroy the object. The Initialize and Terminate events in CZipUp.cls contain some debug code to illustrate this.

Test App Shows You How
Here's how to use ZipUp.dll in your own projects. You can follow along and build your own zip app using ZipUpTest as a reference; download it at http://www.devx.com. ZipUpTest is a Standard EXE project with a reference set to the sample's ZipUp.dll component through the Project | References menu item. You add the Microsoft Common Dialog Control and Microsoft Windows Common Controls to the VB Toolbox using the Project | Components menu item. You use the Common Dialog for browsing files and the Common Control Library's StatusBar control to display the zip operation's status.

ZipUpTest's default form is named frmTest. The app contains a declaration for a project-global zip object, goZip, that can receive events from the ZipUp component:

        Public WithEvents goZip As ZipUp.CZipUp
      

Create the ZipUp object instance with this line of code:

        Set goZip = New ZipUp.CZipUp
      

Destroy the ZipUp object instance with these two lines of code:

        goZip.Reset
Set goZip = Nothing
      

The ZipUpTest app's job is to exercise all properties, methods, and events of the CZipUp class. I always create a test app for the ActiveX DLLs I build—not only does it function as a development platform, but it also serves as documentation by example for the DLL's operation. The main form supports each of the DLL's available functions (see Figure 1). A second form, frmLog (captioned Zip Log), captures the events' output.

 
Figure 1 | Test the ZipUp ActiveX DLL Component. Click here.

Your main goal in creating a wrapper DLL: Make the contained functionality easy to use and reuse. You can check out ZipUp's capabilities easily through VB's Object Browser. Press F2 and select ZipUp in the Object dropdown. Click on CZipUp in the Classes pane to bring up the Members list—15 properties let you set your zip options. Five methods let you add and clear files, start the zip operation, and return strings for any errors that occur. You can also view the two events: Message and FileComplete.

Use ZipUp.dll by following this general sequence of events on the test form: add files, set a zip-file name, choose your zip options, and call the CZipUp class's Zip method. The Message and FileComplete events fire as the zip operation progresses. The Zip method returns when it completes or an error occurs.

You use ZipUpTest's Browse and Add Files buttons to add to the list of files to zip. Code behind the Browse button uses the Common Dialog control to prompt for files. Use the Add Files button to add the selected files with their paths to a collection within the goZip object (see Listing 1). Each button on frmTest is part of a control array named butArray, and the butArray_Click event calls the appropriate code for each button click.

API Helps You Browse
ZipUpTest's Browse buttons choose directory locations for the Zipfile Name and Temporary Directory fields. You browse for a directory in VB with a couple Win32 API calls—I've included a small class named cSHFile.cls to do this for you. The BrowseForTemp routine shows you how to use this helper class:

        Private Sub BrowseForTemp()
'browse for a Temp directory
Dim oSH As New cSHFile
On Error Resume Next
'returns empty string if Cancel clicked
txtArray(kTxtTempDir) = _
   oSH.BrowseForPath("Select Temp Path")
Set oSH = Nothing
End Sub
      

Finally, you call the ZipUp routine when you click on the test app's Zip button (see Listing 2). In ZipUp, you set each available property from the form's checkboxes and textboxes, and you call the CZipUp.Zip method. You get back a status value of zero for success and nonzero for an error. Use the CZipUp.GetErrorDescription method to translate the error status values to text strings.

 
Figure 2 | See the Results of Zip Operations. Click here.

During the zip process, you get messages back from the Info-ZIP DLL through CZipUp's Message and FileComplete events. ZipUpTest displays these messages on frmLog, which contains a listbox and two buttons: one to clear the listbox, and one to dismiss the form (see Figure 2). frmLog's design-time properties make it act like a dialog box: ControlBox=False, BorderStyle=1-Fixed Single, and StartupPostion=1-CenterOwner. The Close button's Default property is set to True. This routine in frmLog adds callback status messages to the listbox:

        Public Sub AddLog (ByVal LogLine As String)
'adds a line to the listbox
lstZipLog.AddItem Trim$(LogLine)
End Sub
      

The AddLog sub is called from within the event routines with code like this:

        Private Sub goZip_Message (ByVal MessageText As String)
'handle message returns from Zip
frmLog.AddLog MessageText
ShowStatus "Processing: " & MessageText
End Sub
      

The detail and number of messages a zip operation returns vary by how you set the form's Quiet and Verbose options. The ShowStatus call in the Message event routine shown in the previous snippet displays the same message on frmTest's status bar. The Show Log button on frmTest displays frmLog.

The Info-ZIP package contains almost no documentation showing how to use the Info-ZIP DLLs. You must experiment with the ZipUpTest app to find out how ZipUp.dll works when you combine the various options. Note that calling into the Info-ZIP ZipUp23.dll doesn't seem to work reliably, but this might arise from some loose ends in the sample code. For example, setting a Temp directory seems to cause intermittent errors. frmTest also contains some code to ensure you use the RootDir property always with the Recurse option (otherwise, the app hangs when you specify files to zip using wildcards).

Once you learn how the ZipUp.dll component behaves when you drive it from ZipUpTest, you can run the ZipUp.dll code in design mode to step through its operation and learn how it works internally. The easiest way to do this: Create a project group, close the ZipUpTest application project and open the ZipUp.dll project, use the File | Add Project menu item to add the ZipUpTest project, and save the project group as ZipUpGroup. Now you can step into the ZipUp.dll code from ZipUpTest to see how it operates.

CZipUp properties expose about half the available underlying ZipUp23.dll options. These exposed options are the ones I've found to be most useful in a VB zip application. You can implement the others by adding CZipUp properties to set the m_yOptions structure values as defined in the ZipOptionsStruct type. For any new properties you add to the class, be sure to add controls to the ZipUpTest form to exercise the new features. Also, put the ZipUp23.dll file in the System directory—if you don't, it won't create the zip file and won't generate an error.

So you've seen how to add some zip to your own VB apps using ZipUp.dll. With some imagination, you can gather files automatically to send to customers or the home office. You can also create your own custom data storage processes. Best of all, you can automate some of your uninteresting archival maintenance tasks that suck up your valuable time.

Stan Schultes is a project and IT manager, and a VB/Web enterprise application architect and developer in Sarasota, Fla. Stan is an MCP in VB and spoke on VB development at Microsoft's DevDays conference. He is a contributing editor for VBPJ and writes regularly for the magazine. Reach Stan at stan@vbexpert.com.