Here you can find the source of zip(File source, File target)
public static void zip(File source, File target) throws IOException
//package com.java2s; /*************************************************************************** * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * //from w w w.j a v a 2 s . co m * Contributors: * Eike Stepper - initial API and implementation **************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zip(File source, File target) throws IOException { zip(source, target, false); } public static void zip(File source, File target, boolean excludeRoot) throws IOException { ZipOutputStream zos = null; try { File root = excludeRoot ? source : source.getParentFile(); int prefixLength = root.getAbsolutePath().length() + (excludeRoot ? 1 : 0); zos = new ZipOutputStream(new FileOutputStream(target)); zipRecurse(source, prefixLength, zos); zos.flush(); } finally { close(zos); } } private static void zipRecurse(File file, int prefixLength, ZipOutputStream zos) throws IOException { ZipEntry entry = null; try { String name = file.getAbsolutePath() + (file.isDirectory() ? "/" : ""); name = name.substring(prefixLength); if (name.length() > 0) { entry = new ZipEntry(name); zos.putNextEntry(entry); if (file.isFile()) { FileInputStream fis = null; try { fis = new FileInputStream(file); copy(fis, zos, 4096); } finally { close(fis); } } } } finally { if (entry != null) { zos.closeEntry(); } } if (file.isDirectory()) { File[] children = file.listFiles(); for (int i = 0; i < children.length; i++) { File child = children[i]; zipRecurse(child, prefixLength, zos); } } } /** * No exception <code>InputStream</code> close method. */ public static void close(InputStream is) { if (is != null) { try { is.close(); } catch (Exception ignore) { ignore.printStackTrace(); } } } /** * No exception <code>OutputStream</code> close method. */ public static void close(OutputStream os) { if (os != null) { try { os.close(); } catch (Exception ignore) { ignore.printStackTrace(); } } } /** * No exception <code>java.io.Reader</code> close method. */ public static void close(Reader rd) { if (rd != null) { try { rd.close(); } catch (Exception ignore) { ignore.printStackTrace(); } } } /** * No exception <code>java.io.Writer</code> close method. */ public static void close(Writer wr) { if (wr != null) { try { wr.close(); } catch (Exception ignore) { ignore.printStackTrace(); } } } /** * Copy chars from a <code>Reader</code> to a <code>Writer</code>. * * @param bufferSize * Size of internal buffer to use. */ public static void copy(Reader input, Writer output, int bufferSize) throws IOException { char buffer[] = new char[bufferSize]; int n = 0; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); } } public static void copy(InputStream input, OutputStream output, byte buffer[]) throws IOException { int n = 0; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); } } public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException { copy(input, output, new byte[bufferSize]); } public static void copy(InputStream input, OutputStream output) throws IOException { copy(input, output, 4096); } public static void copy(File input, File output) throws IOException { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(input); fos = new FileOutputStream(output); copy(fis, fos); } finally { close(fis); close(fos); } } }