Here you can find the source of zipDir(File dir, ZipOutputStream zos, String prefix)
private static void zipDir(File dir, ZipOutputStream zos, String prefix)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 { /**/*w w w.j a va 2 s . co m*/ * Recursively zips all files in "dir" and its subdirectories into the given * ZipOutputStream "zos" using the given path prefix for their names */ private static void zipDir(File dir, ZipOutputStream zos, String prefix) { File[] entries = dir.listFiles(); for (int i = 0; i < entries.length; i++) { if (entries[i].isDirectory()) { // generate directory entry ZipEntry zi = new ZipEntry(prefix + "/" + entries[i].getName() + "/"); try { zos.putNextEntry(zi); zos.closeEntry(); } catch (IOException ioex) { ioex.printStackTrace(); } zipDir(entries[i], zos, prefix + "/" + entries[i].getName()); } else { FileInputStream fis = null; try { fis = new FileInputStream(entries[i]); ZipEntry zi = new ZipEntry(prefix + "/" + entries[i].getName()); zos.putNextEntry(zi); copystream(fis, zos); zos.closeEntry(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ioex) { ioex.printStackTrace(); } finally { try { if (fis != null) fis.close(); } catch (Exception e) { } } } } } /** * Copies the input stream to the output stream using a 1 kB buffer * @throws IOException */ private static void copystream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); } }