Reading a JAR or Zip Archive


// reading a zip file ZipFile file = new ZipFile(filename); Enumeration entries = file.entries(); while (entries.hasMoreElements()) {     ZipEntry entry = (ZipEntry)entries.nextElement();     if (entry.isDirectory()) {         // process directory     }         else {              // process file         } } file.close();



Java has built-in support for reading and writing Zip archive files. A JAR file is a Zip file that contains specified content, so you can also use the Zip file classes and methods to read a JAR file. The zip classes are contained in the java.util.zip package, which is part of the standard JDK. In this phrase, we first create a ZipFile object by passing the filename of an existing Zip file to the constructor of the ZipFile class. We then get all the Zip file's entries into an enumeration type by calling the enTRies() method of the ZipFile object. Once we have the Zip file entries as an enumeration, we can step through the entries and instantiate a ZipEntry object for each entry. From the ZipEntry object, we can determine if the particular entry being processed is a directory or a file. Based on that knowledge, we could then process the entry appropriately.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net