Here you can find the source of ZipFiles(File zip, File... srcFiles)
public static void ZipFiles(File zip, File... srcFiles) throws IOException
//package com.java2s; 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 ZipFiles(File zip, File... srcFiles) throws IOException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip)); ZipFiles(out, "backup", srcFiles); out.close();/*from w w w .j a v a2 s. c om*/ } private static void ZipFiles(ZipOutputStream out, String path, File... srcFiles) { path = path.replaceAll("\\*", "/"); if (!path.endsWith("/")) { path += "/"; } byte[] buf = new byte[1024]; try { for (int i = 0; i < srcFiles.length; i++) { if (srcFiles[i].isDirectory()) { File[] files = srcFiles[i].listFiles(); String srcPath = srcFiles[i].getName(); srcPath = srcPath.replaceAll("\\*", "/"); if (!srcPath.endsWith("/")) { srcPath += "/"; } out.putNextEntry(new ZipEntry(path + srcPath)); ZipFiles(out, path + srcPath, files); } else { FileInputStream in = new FileInputStream(srcFiles[i]); out.putNextEntry(new ZipEntry(path + srcFiles[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } } catch (Exception e) { e.printStackTrace(); } } }