import java.io.*; import java.util.*; import java.util.zip.*; public class ZipExtract { /** * main() */ public static void main(String[] args) throws IOException { // Get the zip name and the entry name. String zipName = args[0]; String entryName = args[1]; // Open the zip. ZipFile zip = new ZipFile(zipName); System.out.println(zipName + " opened."); try { // Get the entry and its input stream. ZipEntry entry = zip.getEntry(entryName); // If the entry is not null, extract it. Otherwise, print a // message. if (entry != null) { // Get an input stream for the entry. InputStream entryStream = zip.getInputStream(entry); try { // Create the output file (clobbering the file if it exists). FileOutputStream file = new FileOutputStream(entry.getName()); try { // Allocate a buffer for reading the entry data. byte[] buffer = new byte[1024]; int bytesRead; // Read the entry data and write it to the output file. while ((bytesRead = entryStream.read(buffer)) != -1) { file.write(buffer, 0, bytesRead); } System.out.println(entry.getName() + " extracted."); } finally { file.close(); } } finally { entryStream.close(); } } else { System.out.println(entryName + " not found."); } // end if } finally { zip.close(); System.out.println(zipName + " closed."); } } }