Here you can find the source of zipFile(ZipOutputStream out, File file, String dir)
private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException
//package com.java2s; /*//from ww w. ja v a 2s . co m * This source code is licensed under the MIT-style license found in the * LICENSE file in the root directory of this source tree. */ import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException { if (file.isDirectory()) { out.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(out, files[i], dir + files[i].getName()); } } else { FileInputStream fis = new FileInputStream(file); out.putNextEntry(new ZipEntry(dir)); int tempByte; byte[] buffer = new byte[1024]; while ((tempByte = fis.read(buffer)) > 0) { out.write(buffer, 0, tempByte); } fis.close(); } } }