import java.io.*; import java.util.*; import java.util.zip.*; public class ZipCreate { /** * main() */ public static void main(String[] args) throws IOException { // Create the zip file, using the first command line argument as the // file name (clobbering the file if it exists). String zipName = args[0]; ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipName)); System.out.println(zipName + " created."); try { // Allocate a buffer for reading the input files. byte[] buffer = new byte[1024]; int bytesRead; // Loop through the file names provided on the command-line. for (int i = 1; i < args.length; i ++) { // Get the file name. String fileName = args[i]; try { // Open the file. FileInputStream file = new FileInputStream(fileName); try { // Create a zip entry and add it to the zip. ZipEntry entry = new ZipEntry(fileName); zip.putNextEntry(entry); // Read the file the file and write it to the zip. while ((bytesRead = file.read(buffer)) != -1) { zip.write(buffer, 0, bytesRead); } System.out.println(entry.getName() + " added."); } catch (Exception ex) { System.out.println(ex); } finally { file.close(); } } catch (IOException ex) { System.out.println(ex); } } } finally { zip.close(); System.out.println(zipName + " closed."); } } }