Here you can find the source of zip(File dir, File base, ZipOutputStream out)
private static void zip(File dir, File base, ZipOutputStream out) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void zip(File dir, File base, ZipOutputStream out) throws IOException { File[] files = dir.listFiles(); byte[] buffer = new byte[8192]; final String FILE_SEP = System.getProperty("file.separator"); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zip(files[i], base, out); // recursively zip sub-directories } else { FileInputStream fin = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry( files[i].getPath().substring(base.getPath().length() + 1).replace(FILE_SEP, "/")); out.putNextEntry(entry); int bytes_read = 0; while ((bytes_read = fin.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); }//from w w w .j a v a 2 s. c om fin.close(); } } } private static void zip(File[] files, ZipOutputStream out) throws IOException { byte[] buffer = new byte[8192]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zip(files[i], files[i], out); // recursively zip sub-directories } else { FileInputStream fin = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath()); out.putNextEntry(entry); int bytes_read = 0; while ((bytes_read = fin.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } fin.close(); } } } private static void zip(File[] files, File base, ZipOutputStream out) throws IOException { byte[] buffer = new byte[8192]; final String FILE_SEP = System.getProperty("file.separator"); for (int i = 0; i < files.length; i++) { File file_on_disk = new File(base.getPath() + FILE_SEP + files[i].getPath()); if (file_on_disk.isDirectory()) { zip(file_on_disk, base, out); // recursively zip sub-directories } else { FileInputStream fin = new FileInputStream(file_on_disk); ZipEntry entry = new ZipEntry(files[i].getPath().replace(FILE_SEP, "/")); out.putNextEntry(entry); int bytes_read = 0; while ((bytes_read = fin.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } fin.close(); } } } }