import java.io.*;
import java.util.*;
import java.util.zip.*;public class ZipCompress {
  public static void main(String[] args) {
    try {
      FileOutputStream f =
        new FileOutputStream("test.zip");
      CheckedOutputStream csum =
        new CheckedOutputStream(
          f, new Adler32());
      ZipOutputStream out =
        new ZipOutputStream(
          new BufferedOutputStream(csum));
      out.setComment("A test of Java Zipping");
      // Can't read the above comment, though
      for(int i = 0; i < args.length; i++) {
        System.out.println(
          "Writing file " + args[i]);
        BufferedReader in =
          new BufferedReader(
            new FileReader(args[i]));
        out.putNextEntry(new ZipEntry(args[i]));
        int c;
        while((c = in.read()) != -1)
          out.write(c);
        in.close();
      }
      out.close();
      // Checksum valid only after the file
      // has been closed!
      System.out.println("Checksum: " +
        csum.getChecksum().getValue());
      // Now extract the files:
      System.out.println("Reading file");
      FileInputStream fi =
         new FileInputStream("test.zip");
      CheckedInputStream csumi =
        new CheckedInputStream(
          fi, new Adler32());
      ZipInputStream in2 =
        new ZipInputStream(
          new BufferedInputStream(csumi));
      ZipEntry ze;
      System.out.println("Checksum: " +
        csumi.getChecksum().getValue());
      while((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while((x = in2.read()) != -1)
          System.out.write(x);
      }
      in2.close();
      // Alternative way to open and read
      // zip files:
      ZipFile zf = new ZipFile("test.zip");
      Enumeration e = zf.entries();
      while(e.hasMoreElements()) {
        ZipEntry ze2 = (ZipEntry)e.nextElement();
        System.out.println("File: " + ze2);
        // ... and extract the data as before
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

解决方案 »

  1.   

    package com.liming.tools;import java.io.*;
    import java.util.*;
    import java.util.zip.*;//Compress files and folders
    public class Compressor {
      /**
       * This seperator is used for ZipEntr.
       */
      private static final String SEPERATOR = "/";  /**
       * Use for Public method zip.
       * @param out
       * @param f zip file
       * @param base
       * @throws java.lang.Exception
       */
      // this Method zip files to the same Stream
      protected static void zip(ZipOutputStream out, File f, String base) throws
          Exception {
        System.out.println(" ... Now Zipping : " + f.getPath());
        // one folder
        if (f.isDirectory()) {
          File[] fl = f.listFiles();
          //put folder entry in out stream
          out.putNextEntry(new ZipEntry(base + SEPERATOR));
          base = base.length() == 0 ? "" : base + SEPERATOR;
              // zip all files in folder
          for (int i = 0; i < fl.length; i++) {
            //Pass and return
            zip(out, fl[i], base + fl[i].getName());
          }
        }
        // one file
        else {
          out.putNextEntry(new ZipEntry(base));
          //put file entry in out stream
          FileInputStream in = new FileInputStream(f);
          int buf;
          // write to zipfile by outStream
          while ( (buf = in.read()) != -1) {
            out.write(buf);
          }
          in.close();
        }  }  /**
       * Compress a folder and all its files, or compress one file.
       * @param zipFileName The zip file name.
       * @param fromFileName The source folder name or file name.
       * @throws java.lang.Exception
       */
      public static void zip(String fromFileName, String zipFileName) throws
          Exception {
        File fromFile = new File(fromFileName);
    // create outStream
        FileOutputStream fout = new FileOutputStream(zipFileName);
        //CheckedOutputStream csum = new CheckedOutputStream(fout, new CRC32());
        //ZipOutputStream out = new ZipOutputStream(csum);
        ZipOutputStream out = new ZipOutputStream(fout);
        System.out.println("--- zip start!! ---");
        String folder = "";
        //file or folder's name  by  java.lang.String folder we save the route
        folder = fromFileName.substring(fromFileName.lastIndexOf(SEPERATOR),
                                        fromFileName.length());
        zip(out, fromFile, folder);
        System.out.println("--- zip done!! ---");
    //update Stream
        out.flush();
    // close Stream
        out.close();  }  /**
       * Decompress a zip file into a folder.
       * @param zipFileName The zip file name.
       * @param outputDirectory The folder contains files decompressed.
       * @throws java.lang.Exception
       */  public static void unzip(String zipFileName, String outputDirectory) throws
          Exception {
        FileInputStream fin = new FileInputStream(zipFileName);
        //CheckedInputStream csum = new CheckedInputStream(fin, new CRC32());
        //ZipInputStream in = new ZipInputStream(csum);
        // read zipfile to inputStream
        ZipInputStream in = new ZipInputStream(fin);
        ZipEntry z;
        while ( (z = in.getNextEntry()) != null) {
          System.out.println(" ... Now unziping : " + z.getName());
          // the entry is one folder.
          if (z.isDirectory()) {
            String name = z.getName();
            name = name.substring(0, name.length() - 1);
            //you must create folder for the file before unzip the file
            File f = new File(outputDirectory + File.separator + name);
            f.mkdir();
          }
          // the entry is one file
          else {
            File f = new File(outputDirectory + File.separator + z.getName());
            f.createNewFile();
            FileOutputStream out = new FileOutputStream(f);
            int buf;
            // write to purpose file
            while ( (buf = in.read()) != -1) {
              out.write(buf);
            }
            out.close();
          }
        }    in.close();
      }  /**
       * Test method.
       * @param args
       * @throws java.lang.Exception
       */
      public static void main(String args[]) throws Exception {
    //    zip("c:/UnZipApplet.class", "f:/aaa.zip");
    //        zip("c:/怴婯","c:/aaaa.zip");
        unzip("f:/aaa.zip", "f:/");
      }}