Zip : Java Glossary
home Z words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-03-27 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 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 (all)
rar Zip
The word zip refers both to American postal codes and 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.

Zip Postal Codes Nesting
Zip File Format GZIP vs Zip
Gotchas Encryption
Writing Learning More
Reading Links
Directories

Zip Postal Codes

Zip 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 on-line zip code lookup. Soap Service gives you state, latitude and longitude given the zip.

Zip File Format

Zip files and jars have a similar format. Each element is preceded 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.

Apache VFS gives you a common API for files that works both for regular files and zip file members. Normally you do your work with ZipFile, ZipEntry. ZipInputStream and ZipOutputStream or for simpler takes GZIPInputStream and GZIPOutputStream.

Writing Elements of a Zip File

Here is how to write a zip file with a single compressed element:
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.

Reading elements of a Zip File

The following code won’t work if ZipOutputStream was used to create the zip file. ZipEntry.getSize will return -1. ZipFile.size() will return the number of elements in the zip.
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.

Directories

Normally directories are not explicitly created or even stored as separate entries in a zip file. When the file is extracted, any directories needed to contain the extracted files are automatically created as needed. However, you can store empty directories in a zip file. They appear as filenames ending in /.

Nesting

The member files in a zip file can be accessed individually, just like the files in a jar file (a species of zip file). However, when one zip is contained within another zip, you can only access the contained zip file itself, not its individual members. You would need to expand it to disk somewhere before accessing its members.

There are three approaches to the problem:

  1. Put all members in the same jar/zip.
  2. Use several individual jar files, and arrange to have them on the path.
  3. Use a JAWS installer class to unpack a nested jar into individual jars.
Why would you nest?

Gotchas

GZIP vs Zip

GZIP is a more primitive file format than zip. GZIPInputStream and GZIPOutputStream let you read and create compressed files, but not using the zip directory structure. The file consists of just one compressed lump, without any embedded members filenames, timestamps etc. For sample GZIPOutputStream code, consult the File I/O Amanuensis.

encryption

Winzip supports: Java’s ZipEntryStream does not support the Winzip compression scheme. So you must manually encrypt and decrypt either on the plaintext or the compressed form perhaps using JCE. Unfortunately you will need your code at both ends to encrypt/decrypt. You won’t be able to create encrypted files that WinZip can decrypt on its own.

Learning More

Sun’s Javadoc on the ZipFile class : available:
Sun’s Javadoc on the ZipEntry class : available:
Sun’s Javadoc on the ZipInputStream class : available:
Sun’s Javadoc on the ZipOutputStream class : available:
Sun’s Javadoc on the GZIPInputStream class : available:
Sun’s Javadoc on the GZIPOutputStream class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] Spread the Net
You are visitor number 35,365.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/zip.html J:\mindprod\jgloss\zip.html