Here you can find the source of zip(File src, File target)
public static void zip(File src, File target) throws IOException
//package com.java2s; /*//from w w w. j a v a2s.c om * z2env.org - (c) ZFabrik Software KG * * Licensed under Apache 2. * * www.z2-environment.net */ import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static FileFilter ALL_FILES = new FileFilter() { public boolean accept(File pathname) { return true; } }; /** * zip a folder to a file */ public static void zip(File src, File target) throws IOException { zip(src, null, target, ALL_FILES); } /** * zip a folder to a file */ public static void zip(File src, File target, FileFilter filter) throws IOException { zip(src, null, target, filter); } /** * zip a folder to a file */ public static void zip(File src, String path, File target) throws IOException { zip(src, path, target, ALL_FILES); } /** * zip a file or folder to a file */ public static void zip(File src, String path, File target, FileFilter filter) throws IOException { OutputStream out = new FileOutputStream(target); ZipOutputStream zo = new ZipOutputStream(out); try { _zip(src, (path == null ? "" : path), zo, filter); } finally { zo.close(); } } private static void _zip(File src, String path, ZipOutputStream zo, FileFilter filter) throws IOException { if (src.isDirectory()) { _addFolderToZIP(src, path, zo); String p; for (File f : src.listFiles(filter)) { p = (path.length() > 0 ? path + "/" : "") + f.getName(); _zip(f, p, zo, filter); } } else { _addFileToZIP(src, path, zo); } } private static void _addFolderToZIP(File f, String path, ZipOutputStream zo) throws IOException { if (path.length() > 0) { path = path + "/"; // append to make it appear as a folder ZipEntry ze = new ZipEntry(path); if (f != null) { ze.setTime(f.lastModified()); } zo.putNextEntry(ze); zo.closeEntry(); } } private static void _addFileToZIP(File f, String path, ZipOutputStream zo) throws IOException { ZipEntry ze = new ZipEntry(path); zo.putNextEntry(ze); ze.setTime(f.lastModified()); int len = (int) f.length(); byte[] buffer = new byte[len]; // read that file completely InputStream fin = new FileInputStream(f); int l, s = 0; while ((l = fin.read(buffer, s, len - s)) > 0) s += l; fin.close(); zo.write(buffer); zo.closeEntry(); } }