将目录压缩到zip输出流
/** * e-Science Central * Copyright (C) 2008-2013 School of Computing Science, Newcastle University * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation at: * http://www.gnu.org/licenses/gpl-2.0.html * * 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., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA. */ //package com.nowjava; /** * n o w j a v a . c o m 提 供 **/ import java.io.*; import java.util.zip.*; public class Main { /** Zip a directory to a zip output stream */ public static void zipDir(String baseDir, String dir2zip, ZipOutputStream zos) throws IOException { //create a new File object based on the directory we have to zip File zipDir = new File(dir2zip); //get a listing of the directory content String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; //loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = new File(zipDir, dirList[i]); if (f.isDirectory()) { /**来自 NowJava.com - 时代Java**/ //if the File object is a directory, call this //function again to add its content recursively String filePath = f.getPath(); zipDir(baseDir, filePath, zos); //loop again continue; } //if we reached here, the File object f was not a directory //create a FileInputStream on top of f FileInputStream fis = new FileInputStream(f); // create a new zipentry ZipEntry anEntry = new ZipEntry(subtractPath(baseDir, f.getPath()).replace("\\", "/")); //place the zip entry in the ZipOutputStream object zos.p