Here you can find the source of zipDir(File zipDir, ZipOutputStream zos, boolean recurse)
Parameter | Description |
---|---|
zipDir | the origin directory |
zos | the ZipOutputStream output |
recurse | should we recurse in subdirs |
Parameter | Description |
---|---|
IOException | an exception |
public static void zipDir(File zipDir, ZipOutputStream zos, boolean recurse) throws IOException
//package com.java2s; /******************************************************************************* * * * Copyright (C) 2007, 2010 - The University of Liverpool * * 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 3 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, see <http://www.gnu.org/licenses/>. * *//w w w .j a va 2 s . com * * Author: Fabio Corubolo * * Email: corubolo@gmail.com * *******************************************************************************/ 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 { /** Utility to compress the contents of a Directory to a ZipOutputStream, if necessary recursing * * @param zipDir the origin directory * @param zos the ZipOutputStream output * @param recurse should we recurse in subdirs * @throws IOException */ public static void zipDir(File zipDir, ZipOutputStream zos, boolean recurse) throws IOException { String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; for (String element : dirList) { File f = new File(zipDir, element); if (f.isDirectory()) { if (recurse) zipDir(f, zos, recurse); // loop again continue; } FileInputStream fis = new FileInputStream(f); String en; if (recurse) en = f.getPath(); else en = f.getName(); ZipEntry anEntry = new ZipEntry(en); zos.putNextEntry(anEntry); while ((bytesIn = fis.read(readBuffer)) != -1) zos.write(readBuffer, 0, bytesIn); fis.close(); } } }