FileDocCategorySizeDatePackage
Zipper.javaAPI DocExample1356Fri Feb 10 13:02:06 GMT 2006None

Zipper

public class Zipper extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

  
    if (args.length < 2) {
      System.out.println("Usage: java Zipper [-d level] name.zip"+
                         " file1 file2...");
      return; 
    }
    
    String outputFile = args[0];
    // Default to maximum compression
    int level = 9;
    int start = 1;
    if (args[0].equals("-d")) {
      try {
        level = Integer.parseInt(args[1]);
        outputFile = args[2];
        start = 3;
      }
      catch (Exception ex) {
        System.out.println("Usage: java Zipper [-d level] name.zip"
                        + " file1 file2...");
        return;         
      }
    }    
    
    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);
    for (int i = start; i < args.length; i++) {
      ZipEntry ze = new ZipEntry(args[i]);
      FileInputStream fin = new FileInputStream(args[i]);
      try {
        System.out.println("Compressing " + args[i]);
        zout.putNextEntry(ze);
        for (int c = fin.read(); c != -1; c = fin.read()) {
          zout.write(c);
        }
      }
      finally {
        fin.close();
      }
    }
    zout.close();