Here you can find the source of zipDir(File zipDir, ZipOutputStream zos, String name)
static void zipDir(File zipDir, ZipOutputStream zos, String name) throws IOException
//package com.java2s; /*/*from w ww .j a v a2 s . c o m*/ * Copyright (C) 2011 Peransin Nicolas. * Use is subject to license terms. */ 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 { public static final String ZIP_FILE_SEPARATOR = "/"; public static final int BUFFER_SIZE = 2156; public static final boolean DEBUG = false; static void zipDir(File zipDir, ZipOutputStream zos, String name) throws IOException { // Create a new File object based on the directory we have to zip if (name.endsWith(File.separator)) name = name.substring(0, name.length() - File.separator.length()); if (!name.endsWith(ZIP_FILE_SEPARATOR)) name = name + ZIP_FILE_SEPARATOR; // Place the zip entry in the ZipOutputStream object // Get a listing of the directory content File[] dirList = zipDir.listFiles(); if (dirList.length == 0) { // empty directory if (DEBUG) System.out.println("Add empty entry for directory : " + name); ZipEntry anEntry = new ZipEntry(name); zos.putNextEntry(anEntry); return; } // Loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = dirList[i]; String fName = name + f.getName(); if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively zipDir(f, zos, fName); } else { zipFile(f, zos, fName); } } return; } static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException { // if we reached here, the File object f was not a directory // create a FileInputStream on top of f FileInputStream fis = new FileInputStream(zipfile); try { // create a new zip entry ZipEntry anEntry = new ZipEntry(name); if (DEBUG) System.out.println("Add file : " + name); // place the zip entry in the ZipOutputStream object zos.putNextEntry(anEntry); // now write the content of the file to the // ZipOutputStream byte[] readBuffer = new byte[BUFFER_SIZE]; for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis.read(readBuffer)) { zos.write(readBuffer, 0, bytesIn); } } finally { // close the Stream fis.close(); } } }