Compressing Streams: Zipper, Java example : ZipOutputStream « File « Java Tutorial






import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

  public static void main(String[] args) throws IOException {
    String outputFile = "a.zip";
    int level = 9;
    FileOutputStream fout = new FileOutputStream(outputFile);
    ZipOutputStream zout = new ZipOutputStream(fout);
    zout.setLevel(level);

    ZipEntry ze = new ZipEntry("a.zip");
    FileInputStream fin = new FileInputStream("b.dat");
    zout.putNextEntry(ze);
    for (int c = fin.read(); c != -1; c = fin.read()) {
      zout.write(c);
    }
    fin.close();
    zout.close();
  }
}








11.60.ZipOutputStream
11.60.1.Create ZipOutputStream from FileOutputStream
11.60.2.Compressing Streams: Zipper, Java example