Make Your Own Zip Command

by John Lansdale

Compiling an Open Source Project from Source—In Windows

While working on a web application in Windows XP that usually runs in Linux, I discovered something missing. The command for compressing files, “zip”, is available in almost all Linux distro’s but not Windows XP. I could comment it out, etc., but making temporary patches in semi-live code just for testing isn’t the best idea. The test isn’t exact and it may be forgotten. I needed a real zip.exe (command) with the same Linux options.

Figure 1. The PHP program expecting a zip command

system("zip -j $filepath.zip $dir/* >/dev/null"); // run the zip command
rename("$filepath.zip", "$html_root/$filename.zip"); // rename the .zip file

Yes, Windows XP has SentTo Compressed folders, and yes, my system has WinZip. I even have 7-zip and bzip2.exe, but nothing named "zip” that creates “.zip” files and has the same Linux options. The evaluation version of WinZip won’t even run from the command line. Winzip32.exe pops up a window. I needed the real thing.

After some search I found the Open Source project I needed, Zip For Windows at http://gnuwin32.sourceforge.net/packages/zip.htm.

It had the zip binary (.exe) I needed but being somewhat unsure of its exact compatibility with the Linux version assumed in my PHP program, I decided to download and compile the whole source file. Now maybe this was just an excuse because I could have just downloaded and tried the binary. However what if, like so many of the other zip-alike programs on my system, it didn’t work. Oh, and I could review the source looking for any virus/spyware—very important. Besides, having run gcc (the standard C++ compiler) in Linux enough times, I was curious if it worked in Windows.

The answer is yes. A little different from Linux but one can compile open source C++ projects in Windows without paying any money. It is fairly fast and easy to do. No C++ knowledge is needed. Here’s how:

Download the Zip for Windows source files at: http://gnuwin32.sourceforge.net/packages/zip.htm and unpack. Note the folder.

Next get a C compiler and where better than Microsoft? Free Visual Studio Express; Visual Studio C++ can be downloaded at http://www.microsoft.com/express/download/ . Yes, this is Microsoft giving away something that not long ago cost over $400. They also have C#, VB.NET, ASP.NET and SQL Server 2005, enough to do just about any project you want. I think they were pushed into it by IBM’s incredibly good, also free GUI source editor Eclipse and a plethora of other good programming environments. You have to register after 30 days to get the software key. I’ve done it for C# and Microsoft doesn’t seem to be too intrusive (i.e. no junk mail). Setup was easy.

Hardest part is finding the main C++ “zip” project inside the unpacked files. Its name is zip.vcproj, mine was way down in C:\gun32\src\zip\2.31\zip-2.31\win32\vc6.

Figure 2. Zip project open in familiar Visual C++ Express Edition

Visual C++ Express

Start C++ and navigate to and open the project and there it is, a familiar old Visual Studio project. Take Build->Build Solution and its ready. No compile errors or anything a novice computer programmer couldn’t fix easily.

The output file zip.exe (binary) goes to: “C:\gnu32\src\zip\2.31\zip-2.31\win32\vc6\zip___Win32_Debug.” The project is still in debug mode. It works fine but is a little fat with reflection information. If you take Build->Configuration Manager and change zip from debug to release (figure 3) the output then goes to “…Win32_Release” and is a bit smaller. You could also change the target folder in Project->Zip Properties.

Figure 3. Change build to release (optional).

Build to release

All you’ve got to do then is either change the system PATH statement to include the new folder, or, easier, copy the .exe to a place other commands are already pointed to; \Windows would be fine. My new zip.exe command worked perfectly, even using the -j option (to only zip top level files) correctly. I was soon back to more mundane PHP debugging.

In the same project there’s source code for zipcloak (encrypting archives), zipnote (for adding comments to a zip file) and zipsplit (for making a multi-volume output, good where space is limited). You may want to learn a little C++ and create your own variation. For example, a Windows Service was triggered every time a file is dropped into a particular folder. The file would automatically be securely compressed and then emailed to a specified location.

Figure 4. Test it out. Compare to man zip (Google it or do it in Linux). It’s quite a close match on options. For me the –j option works as intended.

C:\>zip
Copyright (C) 1990-2005 Info-ZIP
Type 'zip "-L"' for software license.
Zip 2.31 (March 8th 2005). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-! use privileges (if granted) to obtain all aspects of WinNT security
-R PKZIP recursion (see manual)
-$ include volume label -S include system and hidden files
-e encrypt -n don't compress these suffixes

C:\>zip -j mytest.zip zopesite/*
adding: Password.txt (208 bytes security) (stored 0%)
adding: start.bat (208 bytes security) (deflated 10%)
adding: z2.pyc (208 bytes security) (deflated 57%)
adding: Zope.cgi (208 bytes security) (deflated 43%)
adding: zpasswd.pyc (208 bytes security) (deflated 55%)

Now we’re ready to make some changes, compile and change other Open Source projects. Which, just when I finish this PHP project and a couple more, and I learn Java in our Open Source SIG, and I get a little more time to study C++ and you finish up that Java, Cold Fusion, shell script, C#, VBA and COBOL job, we’ll have to get together and work on something.

John Lansdale, CDP, MCP



DacsGear!
Mugs and more, visit CafePress to order
 
 
© Danbury Area Computer Society, Inc. All Rights Reserved.
Web Site Terms & Conditions of Use