Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Unzipping Files with java.util.zip.ZipFile

The Java core packages include jave.util.zip, which is useful for creating and reading compressed files or archives in the ZIP and GZIP formats. This saves you the time of creating custom file archive formats for your applications.


advertisement
he Java core packages include jave.util.zip, which is useful for creating and reading compressed files or archives in the ZIP and GZIP formats. ZIP files form the basis for the JAR file format, so it was natural to include classes for manipulating ZIP files as part of the standard Java APIs. This functionality is enormously useful, because it saves you the time of creating custom file archive formats for your applications. You can use the ZipFile class to read entries in a ZIP file. The entries are represented by instances of the ZipEntry class, which contains information such as the size, name, compression method, and timestamp of the entry. ZipFile.entries() will return the entries as an Enumeration which you can step through to access all of the entries. Once you have found an entry of interest, ZipFile.getInputStream(ZipEntry) will produce an InputStream, from which the archived entry can be read.

The following program demonstrates how to use the ZipFile class to create a simple unzip utility. It simply cycles through all of the entries in the file and copies each entry to a new file by reading its input stream. If an entry is a directory, it is created. The example is not robust and is only intended to demonstrate the ZipFile class. Obvious additions would include checking if a file already exists before creating it, prompting the user if it should be overwritten, as well as other error checking. View the example>:

import java.io.*; import java.util.*; import java.util.zip.*; public class Unzip { public static final void copyInputStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); out.close(); } public static final void main(String[] args) { Enumeration entries; ZipFile zipFile; if(args.length != 1) { System.err.println("Usage: Unzip zipfile"); return; } try { zipFile = new ZipFile(args[0]); entries = zipFile.entries(); while(entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); if(entry.isDirectory()) { // Assume directories are stored parents first then children. System.err.println("Extracting directory: " + entry.getName()); // This is not robust, just for demonstration purposes. (new File(entry.getName())).mkdir(); continue; } System.err.println("Extracting file: " + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(entry.getName()))); } zipFile.close(); } catch (IOException ioe) { System.err.println("Unhandled exception:"); ioe.printStackTrace(); return; } } }





   
Daniel F. Savarese holds a B.S. in astronomy and an M.S. in computer science, both from the University of Maryland, College Park. He is the author of the OROMatcher regular expression library for Java. Reach him here.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap