Here you can find the source of fileToZipFile(File toZip, File output)
public static void fileToZipFile(File toZip, File output)
//package com.java2s; //License from project: Apache License import java.io.File; 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 fileToZipFile(File toZip, File output) { byte[] buffer = new byte[1024]; try {/*w w w . j a v a2s . c o m*/ FileOutputStream fos = new FileOutputStream(output); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry(toZip.getName()); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(toZip); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); //remember close it zos.close(); System.out.println("Done"); } catch (IOException ex) { ex.printStackTrace(); } } }