Here you can find the source of addFileToZip(ZipOutputStream zos, File file, File rootDir)
private static void addFileToZip(ZipOutputStream zos, File file, File rootDir) throws IOException
//package com.java2s; /*//from w ww. j av a 2s.com Copyright (C) 2007 Joao F. (joaof@sourceforge.net) http://paccman.sourceforge.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static final int BUF_SIZE = 256 * 1024; private static void addFileToZip(ZipOutputStream zos, File file, File rootDir) throws IOException { if (file.isFile()) { File relativeFile = new File(file.getAbsolutePath().substring(rootDir.getPath().length() + 1)); ZipEntry ze = new ZipEntry(relativeFile.getPath()); zos.putNextEntry(ze); int count; byte data[] = new byte[BUF_SIZE]; BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file)); while ((count = origin.read(data, 0, BUF_SIZE)) != -1) { zos.write(data, 0, count); } origin.close(); } else { for (File f : file.listFiles()) { addFileToZip(zos, f, rootDir); } } } }