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.
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.
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.
// 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.
home |
Canadian Mind Products | |||
| 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 | |||