Here you can find the source of zipFolder(File root)
public static File zipFolder(File root) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static File zipFolder(File root) throws IOException { if (root == null) return null; if (!root.isDirectory()) return null; File out = File.createTempFile("SHA", null); ZipOutputStream os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(out))); addFolder("", root, os); os.flush();// w w w.j a va 2 s. co m os.close(); return out; } public static File zipFolder(File root, Set<String> topLevelDirFilter) throws IOException { if (root == null) return null; if (!root.isDirectory()) return null; File out = File.createTempFile("SHA", null); ZipOutputStream os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(out))); if (topLevelDirFilter == null) { addFolder("", root, os); } else { addFolder("", root, os, topLevelDirFilter); } os.flush(); os.close(); return out; } private static void addFolder(String path, File folder, ZipOutputStream os, Set<String> topLevelDirFilter) throws IOException { for (File f : folder.listFiles()) { if (f.isDirectory()) { if (topLevelDirFilter != null && !topLevelDirFilter.contains(f.getName())) continue; if (path.equals("")) addFolder(f.getName(), f, os); else addFolder(path + "/" + f.getName(), f, os); } else { byte[] buff = new byte[32 * 1024]; BufferedInputStream is = new BufferedInputStream(new FileInputStream(f)); try { os.putNextEntry(new ZipEntry(path + "/" + f.getName())); int procitano = -1; while ((procitano = is.read(buff)) != -1) { os.write(buff, 0, procitano); } os.flush(); os.closeEntry(); } finally { try { is.close(); } catch (Exception ignorable) { } } } } } private static void addFolder(String path, File folder, ZipOutputStream os) throws IOException { for (File f : folder.listFiles()) { if (f.isDirectory()) { if (path.equals("")) addFolder(f.getName(), f, os); else addFolder(path + "/" + f.getName(), f, os); } else { byte[] buff = new byte[32 * 1024]; BufferedInputStream is = new BufferedInputStream(new FileInputStream(f)); try { os.putNextEntry(new ZipEntry(path + "/" + f.getName())); int procitano = -1; while ((procitano = is.read(buff)) != -1) { os.write(buff, 0, procitano); } os.flush(); os.closeEntry(); } finally { try { is.close(); } catch (Exception ignorable) { } } } } } }