Here you can find the source of addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile) throws Exception
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile) throws Exception { if (srcFile.isDirectory()) { addFolderToZip(path, srcFile, zip, destZipFile); } else if (!srcFile.getName().equals(destZipFile)) { byte[] buf = new byte[1024]; int len; final InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); try { if (path.equals("/")) { zip.putNextEntry(new ZipEntry(srcFile.getName())); } else { zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName())); }/*w w w . ja va 2 s .co m*/ while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } } static private void addFolderToZip(String path, File srcFolder, ZipOutputStream zip, String destZipFile) throws Exception { for (String fileName : srcFolder.list()) { if (path.equals("")) { addFileToZip("/", new File(srcFolder, fileName), zip, destZipFile); } else { addFileToZip(srcFolder.getName(), new File(srcFolder, fileName), zip, destZipFile); } } } }