Introduction to Java :Constructing .Cab File

Building a Cab File

After you have created your ZIP archive, you can then create a CAB (CABinet) file that will contain your compressed and archived applet for Microsoft’s Internet Explorer users. Building CAB files is a little less user-friendly than building ZIP files, since there is no graphical interface like WinZip for creating CAB files.

Instead, you will have to use the command line interface provided by CABARC. CABARC is a compression/archiving program distributed with Microsoft’s SDK located in either the “bin” directory under your main SDK directory or the “bin/packsign” directory.

CABARC follows the generic format:

Options and Commands are summarized in the following table:

 

ParameterCommand
LUsed to list the contents of a cabinet
NUsed to create a cabinet file
XUsed to extract single or multiple files from the cabinet
Options
-cConfirms the files to be operated upon
-oOverwrites without confirming when extracting
-mSets the type of compression to use to LZX:<15..21>, MSZIP, or NONE. The default is MSZIP
-pUsed to preserve path names
-PUsed to strip a specified prefix from files when they are added
-rUsed to recurse into subdirectories when adding files
-sUsed to reserve space in the cabinet for signing. For example, -s 6144 reserves 6k.
-IUsed to set the ID of the cabinet when creating a cabinet. The default is 0.

 

CABARC Commands

As you can see, there are several commands supported by CABARC. You can list files, add files, and remove files from cabinets.

Since you will be most interested in creating CAB files, you will be primarily using the “N” command. For example, the following CABARC command would create a CAB file called “MyApplet.cab” that would contain all of the class files in the current directory:

CABARC also allows you to list the files in a current cabinet. In this case, you will use the “l” command as follows:

The “L” command returns information about the size of each file in the cabinet — the dates and times each was added, as well as the attributes of each file.

CABARC also allows you to selectively remove files from a cabinet using the “X” command. For example, to remove the SupportClass.class file from the example above, we would use the following syntax:

Notice that when extracting files, the default behavior is for CABARC to ask you if you would like to overwrite existing files, if they have the same name as those being extracted.

CABARC Options

You will most likely specify options when performing actions with CABARC. For example, consider the MyApplet example we have been using so far in this chapter. The MyApplet program actually contains several files and several directories. Unfortunately, as shown above, if you simply use the “N” option to create your archive, you will not be able to add all these files and directories in one simple line. You would have to add each directory and file separately.

Fortunately, CABARC provides the -r option to add files to a CAB archive recursively. Thus, to create a functional CAB archive for our applet, we would want to use something along the lines of

As you can see, the -r option will add all of the files and all of the subdirectories necessary for our cabinet file. However, the -r option will add files that are not necessary such as the .txt , .java and .html files. In order to make your CAB archive as small as possible, you will want to only add the necessary files.

To do so, you can specify the list of files to add using something like the following:

In this case, you will only get files with the specified extensions.

Referencing Your Cabinet

However, you are not quite done yet. Once you create your cabinet, you must then upload it to your web server and make it available to web browsers using the CABBASE parameter as follows:

In this case, Internet Explorer will parse the parameter and access the cabinet file.