Add Ons - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Add Ons arrow Page 3 - Zipping Up Code with ZipForge
Zipping Up Code with ZipForge
By Jeff Cogswell

Rate This Article: Add This Article To:

Zipping Up Code with ZipForge - ' Beyond File Creation '
( Page 3 of 4 )

Listing and Searching Zip Files

One feature I've needed for my own development work is a tool to search through multiple zip files to find one containing a particular file. When I program in Java, such a tool is especially useful, since JAR files are ZIP archives.

ADVERTISEMENT

The ZipForge component lets you list the names of the files within a zip archive file. The ZipForge class includes methods that are similar to the find-file methods in C and in Unix. To list the contents, you create a new instance of ZipForge, open the archive, and perform a FindFirst, passing the filename pattern (such as *.txt, for example). Then you loop, each time calling FindNext until you get nothing back.

Here's a sample routine that lists all .doc files in a zip archive:

Sub ListFiles()
    Dim zip As New ZipForge
    zip.FileName = "c:\myfiles.zip"
    zip.OpenArchive(System.IO.FileMode.Open)
    Dim archiveItem As _
        ComponentAce.Compression.ZipForge.ArchiveItem = _
        New ComponentAce.Compression.ZipForge.ArchiveItem()
    If zip.FindFirst("*.doc", archiveItem) Then
        Do
            Console.WriteLine(archiveItem.FileName)
        Loop While zip.FindNext(archiveItem)
    End If
    zip.CloseArchive()
End Sub

Using this code as a starting point, a command-line tool that searches through several zip archives for a particular file (or files) would be easy to write. The .NET framework includes a method called GetDirectories in the System.IO.Directory class. You could call this method recursively, call GetFiles to find the ZIP files within each directory, and then use ZipForge to search each found ZIP file.

Creating Self-Extracting Files

With the ZipForge control, you can create your own self-extracting executable files (also called an SFX). A lot of tools (such as WinZip) provide this functionality as well. However, ZipForge gives you complete control over the process.

Creating a self-extracting executable is a multi-step process. You need to have two projects, one that creates the SFX and one called a stub that gets embedded in the SFX. When you run the SFX, the stub is what runs and, presumably, unzips the file. (Since you write the code for the stub, you also write the code to unzip the files or do whatever you need.)

The code for the stub is pretty simple; you do the usual work of opening a ZIP file and unzipping it. The only catch is the file that you unzip is the self-extracting executable itself, the very one that is running. This can create some locking problems, however, since the file is already open. When I tested the feature, then, what I did was also write code that copies the executable file to a temporary filename, and then unzip that file, like so:

zip.FileName = System.IO.Path.GetTempFileName
System.IO.File.Copy("sfx.exe", zip.FileName, True) 

Remember, this code is in the stub, and it runs when you run the final self-extracting executable, which in this sample code is sfx.exe. Then I used the methods I described earlier in this article to extract the files.

The idea of creating your own self-extracting files is cool, but there was also something strange about the whole thing. Normally, if I need to create a self-extracting executable file, I would rely on a third-party tool, such as one from WinZip. I wouldn't, however, normally write the code myself to create the self-extracting executable file. Such code would exist as a developer's tool. I can, however, see writing my own custom stub code that lives inside the self-extracting executable.

But the ZipForge control can create self-extracting files, provided you write the code to do so. While testing out this feature, I found myself getting a little confused, because I was dealing with two projects and three executables. The two projects were the program that creates the self-extracting executable, and the stub project that does the extracting. The executables were the two for the two projects, plus the final self-extracting executable itself.

As a developer, I can see why I would want to create self-extracting executables, and I think it's a great idea that I be able to write my own stub. But it seemed extra work to also write the program that created the self-extracting executable. And I can't imagine distributing such an application; it seems that a program to create a self-extracting executable would be a small tool that I use, not write and ship. And indeed, the code for the tool was quite simple:

Dim zip As New ZipForge
zip.FileName = "c:\myfiles.zip"
zip.SFXStub = "stub.exe"
zip.MakeSFX("sfx.exe")

However, that's such a small amount of code that writing the tool myself isn't a big deal. And the important feature I see here in ZipForge is being able to write my own stub that runs when the SFX runs. I can even imagine doing things more than unzip the files; for example, you might want to attach several read-only files to an executable that contain important data. These files could be opened and read as a ZIP file attached to the self-extracting executable, and you wouldn't necessarily even unzip these files.



 
 
>>> More Add Ons Articles          >>> More By Jeff Cogswell
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.