2006-09-30
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 4 )
-Invent the Wheel?">
The ZIP file format is popular enough that, sooner or later, you'll probably need to write code that reads and writes zip files. You may need to manipulate several files at once that are stored together in a zip archive file, or you might simply need its compression features.
In most projects, you must consider how much you want to reinvent the wheel. I, for one, wouldn't bother writing my own ZIP compression algorithms, since that's not what I'm in the business of doing. Instead, I'll find a library that does what I need. One such library is ZipForge, from ComponentAce, a company that provides custom libraries and components for Borland and Microsoft.NET products. With ZipForge, you can create zip archive files, add files to an archive, replace files, and delete files. You can also read files from a zip archive into memory, and write the files from memory into a zip archive. Additionally, you can do the traditional work of a zip program, such as unzipping files onto the hard drive. All from within your code.
Creating Zip Files
Before exploring the features in detail, make sure you're clear that ZipForge does more than just compress and decompress data. Lots of algorithms can take an array of data in memory and create a new array consisting of a compressed version of the first array. The .NET framework even includes such classes and algorithms. ZipForge, on the other hand, is a full archiving tool for managing zip archive files.
Creating and managing a zip file is easy. First, you create an instance of the class ZipForge, and set its Filename member. (I used VB.NET for my tests, but C# works equally well, as do any other .NET languages.)
Dim zip As New ZipForge zip.FileName = "c:\myarchive.zip"
You then open the file, passing System.IO.FileMode.Create to create a new file, or System.IO.FileMode.Open to open an existing file:
zip.OpenArchive(System.IO.FileMode.Create)
If you use the Open file mode, you can still write to the archive; ZipForge just won't create a new archive if it doesn't already exist.
To add files to the archive, you specify the directory for the files you're adding, and then calling AddFiles to add all files matching a filename pattern:
zip.BaseDir = "c:\temp"
zip.AddFiles("*.html")
Finally, close the archive:
zip.CloseArchive()
Here's the whole code:
Dim zip As New ZipForge
zip.FileName = "c:\myarchive.zip"
zip.OpenArchive(System.IO.FileMode.Create)
zip.BaseDir = "c:\temp"
zip.AddFiles("*.html")
zip.CloseArchive()
That's pretty easy! As with any zip tool, the ZipForge class can optionally traverse into subdirectories when adding files. Further, you can specify whether to include the full path of the file (for example, c:\temp\subdir\somefile.txt) or just the relative path (for example subdir\somfile.text). To set such options, the ZipForge class has a member called Options that itself has several members to control such things as recursion and full path usage.
You can also extract the files from an archive. Again, you open the file as before, but this time use a file mode of Open and call ExtractFiles instead of AddFiles. You can extract all the files, or a single file by name, or a set of files by a pattern. Here, I extract only files that start with the letter B and end in HTML, and I place the extracted files in a directory called c:\output:
Dim zip As New ZipForge
zip.FileName = "c:\myarchive.zip"
zip.OpenArchive(System.IO.FileMode.Open)
zip.BaseDir = "c:\output"
zip.ExtractFiles("b*.html")
zip.CloseArchive()
Just as you can specify how to store the directories when you create a zip file, you can also specify what to do with directories during unzip. In that case, you have two choices: You can ignore the directories during unzipping and place all the files in a single directory, or you can recreate the directory structure.
Of course, when you extract the files, you might run into the situation of a file already existing. For that, you can set an option called OverwriteMode which gives you plenty of granularity always overwrite existing files, never overwrite, extract if the existing file is newer or older, or prompt the user. To use the last option, ZipForge includes an event called OnConfirmOverwrite, which you can write code to handle.
![]() |
|


