Here you can find the source of zip(File file, File zip)
public static void zip(File file, File zip) throws IOException
//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.List; import java.util.zip.Deflater; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zip(File file, File zip) throws IOException { ZipOutputStream zos = null; try {/*from w ww . j av a 2 s .c o m*/ String name = file.getName(); zos = new ZipOutputStream(new FileOutputStream(zip)); ZipEntry entry = new ZipEntry(name); zos.putNextEntry(entry); FileInputStream fis = null; try { fis = new FileInputStream(file); byte[] byteBuffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = fis.read(byteBuffer)) != -1) { zos.write(byteBuffer, 0, bytesRead); } zos.flush(); } finally { try { fis.close(); } catch (Exception e) { } } zos.closeEntry(); zos.flush(); } finally { try { zos.close(); } catch (Exception e) { } } } public static File zip(List<File> files, String zipFileName) { return zip(files, zipFileName, null); } public static File zip(List<File> files, String zipFileName, String baseFolder) { String folder = ""; if (baseFolder == null || baseFolder.length() < 1) { folder = ""; } else { if (folder.endsWith(File.separator) == false) { folder = baseFolder + File.separator; } } File zipfile = new File(zipFileName); byte[] buf = new byte[1024 * 4]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); out.setLevel(Deflater.BEST_SPEED); for (File file : files) { FileInputStream in = new FileInputStream(file); out.putNextEntry(new ZipEntry(folder + file.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); return zipfile; } catch (IOException ex) { System.err.println(ex.getMessage()); } return null; } }