Here you can find the source of zipSubDirectory(String basePath, File dir, ZipOutputStream zout)
private static void zipSubDirectory(String basePath, File dir, ZipOutputStream zout) throws IOException
//package com.java2s; //License from project: Apache 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 zipSubDirectory(String basePath, File dir, ZipOutputStream zout) throws IOException { byte[] buffer = new byte[4096]; File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { String path = basePath + file.getName() + "/"; zout.putNextEntry(new ZipEntry(path)); zipSubDirectory(path, file, zout); zout.closeEntry();/*from w ww . ja v a2 s. co m*/ } else { FileInputStream fin = new FileInputStream(file); zout.putNextEntry(new ZipEntry(basePath + file.getName())); int length; while ((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } zout.closeEntry(); fin.close(); } } } }