How to Use PKZIP From the Command Line
Iain Laskey PKZips his way through the precursor to Winzip
In a previous article I showed how to use WinZip to compress files. WinZip is the descendent of an earlier program called pkzip. Unlike WinZip, pkzip is a command line based program which means you have to manually type in commands to tell it what to do. To make things worse, it only creates zip files. To unzip them you needed a second program, pkunzip. Earlier versions didn't understand the long filenames introduced with Windows 95 so make sure you get at least version 2.5 of pkzip which is available from our files download area.
Under Windows 95/98/Me, to open a DOS prompt, click on the Start button then select 'MS-DOS Prompt'. The method will be similar under NT and 2000 although it may be referred to as a command prompt .
The Basics
Before we get in to using pkzip, it is a good idea to learn a few basic DOS commands. At the moment, you probably just have something like 'C:\Windows\Desktop\>' in the DOS window and not much else. Type 'dir' and press return. You will see a list of all the files in the current directory. To go up a directory, type 'cd..' and press enter. To go up two directories, type 'cd..\..'. If there is a directory in the current one that you wish to look in, you type cd followed by the directory name e.g. 'cd files'. In this instance, 'files' must be a subdirectory of the current one. You can also look in other directories without changing your current one by typing something like 'dir c:\temp' which will list all the files in the temp directory irrespective of which directory you are in.
The other thing you need to be aware of are the wildcard characters. There are two of these and they are used to specify multiple filenames easily(!) The first is '?' which means 'any single character'. If you wanted to specify all files that had names such as save.001, save.002 and so on, you could enter save.00?. Any files that have save.00 followed by any other single character would be included. The other wildcard is the asterisk. This means 'any combination of characters'. If you wanted all word documents, you could enter '*.doc' which means 'any files as long as the file extension is .doc. Another example is 'Letter*.doc which would include such files as LetterToJon.doc, LetterToBank.doc and so on.
Pkzip
The basic pkzip command is as follows:
Pkzip <options> zipfilename sourcefiles
Ouch! What this means is you type pkzip followed by optional parameter(s) then the name of the zip file you want created and lastly one or more files you want added to the zip. Let's look at a real example:
Pkzip -a backups.zip "C:\My Documents\review.doc"
Here we have the pkzip command then -a which means 'add to zip file' then the name of the zip file, backups.zip and finally the file we want to add to backups.zip. As the filename has a space in it, we have to add quote marks around it otherwise pkzip thinks you are trying to add two files, one called "C:\My" and "Documents\review.doc".
If we wanted to add two files we could use:
Pkzip -a backups.zip "C:\My Documents\review.doc" C:\temp\game.exe
If you want to add a whole directory full of files, you use the DOS wildcard characters e.g.
Pkzip -a backups.zip "C:\My Documents\*.*"
Here you are saying all files in the 'My Documents' directory no matter what the filename or extension is.
If you just wanted Word documents in that directory you'd use:
Pkzip -a backups.zip "C:\My Documents\*.doc"
There are other commands you can use. To move files in to a zip file instead of adding them (a move deletes the original, just adding leaves it there), use -m e.g.
Pkzip -m backups.zip afiletomove.doc
To delete a file from a zip file, use -d
Pkzip -d test.zip mysheet.xls
This would delete a file called mysheet.xls from a zip file called test.zip
see all the possible commands, type pkzip -h for help then press 2 to see the options. There are quite a few and it is worth experimenting to see what each one does. You can always look at the resulting file with WinZip to see if it was what you expected! Typical gotchas include the way subdirectories are processed and you have a few options for how they are to be handled.
Writing Scripts
Now, if WinZip does all the above more easily, why bother with it? The answer is that using pkzip gives you the ability to create scripts or lists of commands. You might want to create a script that automatically takes all your important files, compresses them into a single zip file and copies it somewhere safe. You can create these script or batch files using Windows Notepad. Save them with a .bat file extension. Try this for a taste of what can be done.
Pkzip -a c:\temp\backup.zip "C:\My Documents\*.*"
Pkzip -a c:\temp\backup.zip c:\graphics\*.*
Pkzip -a c:\temp\backup.zip c:\photos\*.jpg
Copy c:\temp\backup.zip r:\
Del c:\temp\backup.zip
The first three lines copy all the files in "C:\My Documents", c:\graphics and any jpg files in c:\photos and puts them all in to a zip file called c:\temp\backup. The first one creates backup.zip as it doesn't already exist and the other two simply add more files. The copy command copies the zip file to the r:\ drive which could be a CD-RW drive or other removable storage and the last line deletes the backup.zip file as it is now safely stored elsewhere (all being well!). Who needs a backup program? You've just written your own, albeit it a rather basic one. You can now create a shortcut on the desktop to the batch file. Wwhenever you want to backup your files, just double click on the shortcut which will automatically fire up a DOS window and run the script. Neat!
Where Next?
I've only shown you the basics here. There are far too many options to cover in a single feature. You might want to include subdirectories, choose different compression systems, add passwords, automate the running of the batch file via the Windows Task Scheduler and more. There is a lot to learn and the best way is to try the above examples, look at the options in pkzip and have a play with it. Enjoy!


