Java tutorial
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { static public File zipFolder(File srcFolder, String destZipFile) throws Exception { final File file = new File(srcFolder, destZipFile); final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file))); try { addFolderToZip("", srcFolder, zip, destZipFile); } finally { zip.close(); } return file; } 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); } } } 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())); } while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } } }