Java Glossary : Zip

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : Z words : Zip.

Zip
PkWare's public domain file archiving and compression format. Sun has extended it in its JAR and WAR files to have a formal table of contents. Beware "class.zip " its members may be archived but not compressed. The classes in package java.util.zip such as ZipFile, ZipInputStream and ZipOutputStream will let you read and create zip or jar files. Don't worry about ZipEntry.setCrc since it and setCompressedSize get set automatically.

In contrast GZipInputStream and GZipOutputStream let you read and create compressed files, but not using the zip directory structure. The file is one compressed lump. For sample code see the FileIO Amanuensis.

Apache VFS gives you a common API for files that works both for regular files and zip file members.

Zip also stands for Zoning Improvement Plan, the American postal code made of a 5+4 numeric. The code is assigned so that you can determine the state from the first three digits of the zip code. The US Post Office has an online zip code lookup. Soap Service to give you state, latitude and longitude given the zip.

Zip File Format

Zip files and jars have a similar format. Each element is preceeded by a header, then there is a summary set of headers at the very end of the file. PKware documents the ZIP file header format.

PkZip and Winzip use / as the directory separator character. It is up to you to convert the \ to / in element names for the ZipEntry write, and back again on read. If you don't bother, the \ will get in the zip file, and you will have a platform-dependent zip.

ZipOutputStream produces a slighly non-standard format. ZipOutputStream puts the compressed and uncompressed size and CRC after the data, instead of in the local header just in front of it. Unfortuately, when you come to read this file with ZipInputStream, when you do an ZipEntry.getSize() you will get 0, because ZipInputStream is a stream, and can't look ahead to find it. There is a second copy of the header put at the end of the file forming an index. ZipFile is able to use this index to randomly access the file to read individual elements.

Writing Elements of a Zip File

To simply write a single compressed file without elements, consult the the File I/O Amanunensis. Here is how to write a zip file with a single compressed element:


view

Reading elements of a Zip File

To simply read a single compressed file without elements, consult the the File I/O Amanunensis. To read just a few elements of a zip file you would use ZipFile. This code won't work if ZipOutputStream was used to create the zip file. ZipEntry.getSize will return 0.

// crude code to unpack a zip file into element files.
FileInputStream fis = new FileInputStream ( "in.zip" );
ZipInputStream zip = new ZipInputStream ( fis );
try
   {
   // loop for each entry
   while ( true )
      {
      ZipEntry entry = zip.getNextEntry();
      // relative name with slashes to separate dirnames.
      String elementName = entry.getName();
      File elementFile = new File( "targetdir" , elementName );
      // checking that subdirs exist for elementname is not shown.
      int fileLength = (int)entry.getSize();
      byte[] wholeFile = new byte[fileLength];
      int bytesRead = zip.read( wholeFile , 0 , fileLength );
      // checking bytesRead is not shown.
      FileOutputStream fos = new FileOutputStream ( elementFile );
      fos.write( wholeFile, 0, fileLength );
      fos.close();
      elementFile.setLastModified ( entry.getTime() );
      zip.closeEntry();
      }
   }
catch ( EOFException e )
   {
   }zip.close();

To read all the elements of a zip, you would use ZipFile.getEntries() to enumerate all the entries. Unfortunately, this enumeration is in "random" order -- Hashtable order really. To efficiently move the disk arms over the file, you really should sort the entries first in the order they appear in the zip.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 4038.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/zip.html J:\mindprod\jgloss\zip.html