This article provides a class to enable you to simply add the ability to zipping
files in a VB application. With this class you can create zips which recurse
subdirectories, store full or relative path names, add and delete files from
zips, freshen zips - basically everything you would expect to be able to do
from a fully featured zipping application, and all under your programmatic control.
Reusing the VB code - Quick Start
To get the simplest overview of the code, extract these two files from the Zip Sample
Add these two files to a new project in VB, and then declare a
WithEvents instance of the cZip class:
Private WithEvents m_cZip As cZip
The simplest operation is to add a single file to a new zip file. This is all you
need to do:
' Set the zip file:
m_cZip.ZipFile = sFile
' Make sure any previously zipped files are cleared:
' Add the name of the file you want to zip:
' Create the zip!
In More Detail
Adding Files to a Zip
Adding files to the zip is accomplished using the AddFileSpec method.
A file specification can either be a fully specified path name (e.g.
C:\Stevemac\HTML\ssite\index.html), a wildcard specification (e.g. C:\Stevemac\HTML\ssite\*.htm? )
or a relative path (e.g. index.html). Relative paths are taken relative to the BasePath
property of the zip. If you wish to store relative directory names in your zip, then you
must specify relative paths as the file spec.
AddFileSpec adds additional file specifications to be used during operations. To clear
the buffer, use ClearFileSpecs. To modify existing file specifications, you can
use FileSpecCount to return how many specifications have been set up and the
property FileSpec to read or write the property.
Progress and Cancel
As Zipping operations are performed, the class will raise the
Progress event, which you can use to display status messages about the
directory operation, and Cancel events, which allows you to stop the directory or unzip
The most important zip options are RecurseSubDirs and StoreFolderNames.
When you set RecurseSubDirs, the zip DLL will check for all files starting at
the BasePath property and below for matches against each of the FileSpecs.
So, for example, if you add "*.*" as a specification, all files in the BasePath
folder and below will be added to the zip. The StoreFolderNames option determines
whether the zip will include the folder names as well as the file names. The default
operation of this option is to store the full folder name, however, if you're FileSpec
items are relative paths at or below the BasePath then the zip will include
the relative path name instead. Note that relative folder names can only be stored for
deeper folders, so ..\MyFolder will not be represented.
Deleting files from a Zip is accomplished in exactly the same way as zipping, except
you use the Delete method instead of the Zip method.
For a more sophisticated zipping sample, check out the VBP Zip
Back to top
Back to Library Source Code
Back to Source Code