Here you can find the source of zipDir(File zipFile, File dirObj)
public static void zipDir(File zipFile, File dirObj) throws Exception
//package com.java2s; //License from project: Open Source 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 zipDir(File zipFile, File dirObj) throws Exception { // File dirObj = new File(dir); if (!dirObj.isDirectory()) { System.err.println(dirObj.getName() + " is not a directory"); System.exit(1);//from w ww. j av a 2s . c o m } try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); System.out.println("Creating : " + zipFile); addDir(dirObj, out); // Complete the ZIP file out.close(); } catch (IOException e) { throw new Exception(e.getMessage()); } } private static void addDir(File dirObj, ZipOutputStream out) throws IOException { File[] dirList = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < dirList.length; i++) { if (dirList[i].isDirectory()) { addDir(dirList[i], out); continue; } FileInputStream in = new FileInputStream(dirList[i].getAbsolutePath()); System.out.println(" Adding: " + dirList[i].getAbsolutePath()); out.putNextEntry(new ZipEntry(dirList[i].getAbsolutePath())); // Transfer from the file to the ZIP file int len; while ((len = in.read(tmpBuf)) > 0) { out.write(tmpBuf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } } }