September 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Accessing zips and jars using Java Part 2

  • January 18, 1999
  • By Thornton Rose
  • Send Email »
  • More Articles »

This is the second half of a two part series on accessing zip and jar files using Java. In part one, we explained how to get the table of contents of a zip file or a jar file and how to get the manifest of a jar file. In this part, we'll show you how to create, add files to, and extract files from zips and jars.

Creating

Creating a zip file is easy. Only three lines of code are required:

ZipOutputStream zip = 
 new ZipOutputStream(new FileOutputStream("simple.zip"));
zip.putNextEntry(new ZipEntry("stub"));
zip.close();

To create a jar file, you use the JarOutputStream and JarEntry classes instead of the ZipOutputStream and ZipEntry classes, like this:

JarOutputStream jar = 
 new JarOutputStream(new FileOutputStream("simple.jar"));
jar.putNextEntry(new JarEntry("stub"));
jar.close();

Notice that in both of the code fragments above we add a stub entry. This is necessary because closing a zip file or a jar file that does not have any entries will throw an exception. Also, note that if the zip file or jar file exists, it will be overwritten.

Adding

Adding files to a zip file requires a little more code than getting the table of contents but is not much more difficult. Here is an example program, ZipCreate, that creates a zip file and adds files to it (like the jar tool does when the "c" switch is specified). Both the zip file name and the names of the files to add are read from command-line arguments.

The algorithm for ZipCreate is:

  1. Create a zip output stream for the specified zip file.
  2. Allocate a buffer for reading the input files.
  3. Loop through the command-line arguments, starting at the second one. For each:
    1. Open the named file.
    2. Create a zip entry for the file.
    3. Add the zip entry to the zip.
    4. Read the file data and write it to the zip output stream. (The data will be compressed automatically using the default compression method.)
    5. Close the file.
  4. Close the zip output stream.

To create a jar file and add files to it, you use the algorithm above, but you use the jar classes instead of the zip classes. An example program for doing this, JarCreate, is here.

Note that methods for adding all files that match a given pattern are not provided by the zip and jar classes. You will have to obtain or write your own code for that.

Extracting

Extracting files from a zip file is no more complicated than adding files. You just use different classes. Here is an example program, ZipExtract, that extracts a given file from a given zip file. Both the name of the zip file and the name of the file to extract are read from command-line arguments.

The algorithm for ZipExtract is:

  1. Get the name of the zip file and the name of the file to extract.
  2. Open the zip file.
  3. Get the zip entry that corresponds to the given file.
  4. If the zip entry is found:
    1. Get an input stream for the zip entry.
    2. Create an output stream for the file to extract.
    3. Allocate a buffer for reading the zip entry data.
    4. Read the zip entry data and write it to the file output stream. (The zip entry data will be decompressed as it is read.)
    5. Close the file output stream.
    6. Close the zip entry input stream. Otherwise, print an error message.
  5. Close the zip file.

To extract a file from a jar file, you use the algorithm above, but use the jar classes instead of the zip classes. An example program for doing this, JarExtract, is here.

Note that in ZipExract and JarExtract I do not check for path names. In a real program, you would want to do this, so that you could create directories as you extract files.

Updating

Updating an existing zip file or jar file gets tricky, because the zip and jar classes do not provide truly random access to zip files and jar files. The ZipFile and JarFile classes have a method for getting a specific entry but not for adding an entry. The ZipInputStream and JarInputStream classes have a method for adding an entry but not for getting a specific entry. So, updating a zip file or a jar file will often require the use of a temporary file.

Here is an example program, JarUpdate, that updates a specific entry in an existing jar file.

The algorithm for JarUpdate is:

  1. Get the jar name and the file name from the command-line arguments.
  2. Create file objects for the jar file and a temporary jar file.
  3. Open the jar file.
  4. Create the temporary jar file, with no manifest. (The manifest will be copied from the original jar file.)
  5. Allocate a buffer for reading file data.
  6. Open the specified file.
  7. Create an entry in the temporary jar for the specified file.
  8. Read the file data and write it to the temporary jar.
  9. Loop through the entries of the original jar file, skipping the entry that was added to the temporary jar. For each entry:
    1. Get an input stream for the entry.
    2. Read the entry data and write it to the temporary jar.
  10. Close the temporary jar file.
  11. Close the original jar file.
  12. If the temporary jar file was created without an exception, delete the original jar file and change the name the temporary file to the name of original jar file. Otherwise, delete the temporary jar file.

To update an existing zip file, you would use the algorithm above but would use the zip classes instead of the jar classes. This is left as an exercise for the reader.

Note that in JarUpdate we assume the default compression method for both the original jar file and the temporary jar file. In a real program, you would want to make sure that the same compression method is used for the entries in both jar files.

Resources

About the author

Thornton Rose is a software developer who lives and works in Atlanta, Georgia. He can be reached via e-mail at trose@avana.net.







Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel