Here you can find the source of zipFolder(String srcFolder, String destZipFile)
static public void zipFolder(String srcFolder, String destZipFile)
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { static public void zipFolder(String srcFolder, String destZipFile) { ZipOutputStream zip = null; FileOutputStream fileWriter = null; try {/*from w w w .ja v a2s.c o m*/ fileWriter = new FileOutputStream(destZipFile); zip = new ZipOutputStream(fileWriter); } catch (Exception ex) { ex.printStackTrace(); return; } addFolderContentsToZip(srcFolder, zip); try { zip.flush(); zip.close(); } catch (Exception ex) { ex.printStackTrace(); } } static private void addFolderContentsToZip(String srcFolder, ZipOutputStream zip) { File folder = new File(srcFolder); String fileListe[] = folder.list(); try { int i = 0; while (i <= fileListe.length) { addToZip("", srcFolder + File.separator + fileListe[i], zip); i++; } } catch (Exception ex) { } } static private void addToZip(String path, String srcFile, ZipOutputStream zip) { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; try { FileInputStream in = new FileInputStream(srcFile); String nextEntryPath = path + "/" + folder.getName(); //This path-separator is hard coded purposely if (path != null && path.trim().equalsIgnoreCase("")) { nextEntryPath = folder.getName(); } zip.putNextEntry(new ZipEntry(nextEntryPath)); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } in.close(); } catch (Exception ex) { ex.printStackTrace(); } } } static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) { File folder = new File(srcFolder); String fileListe[] = folder.list(); try { int i = 0; while (i <= fileListe.length) { String newPath = folder.getName(); if (!path.equalsIgnoreCase("")) newPath = path + "/" + newPath; //This path-separator is hard coded purposely addToZip(newPath, srcFolder + File.separator + fileListe[i], zip); i++; } } catch (Exception ex) { } } }