将网上搜来的资料整理后用于自己项目的工具类,可以压缩当个文件、多层文件夹。注释少了点,但应该看得懂,呵呵 package com.kingkit.vote.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; //import java.util.zip.ZipEntry;//文件名为中文时出现乱码 //import java.util.zip.ZipOutputStream;//文件名为中文时出现乱码 /** * 压缩文件工具 * * @version 1.0 * @author 黄庆勇 * @date 2010-05-08 */ public class ZipUtil { private ZipUtil() { } /** * 压缩文件,支持单个文件以及文件夹 * * @param srcFilePath * 需要压缩的源文件的绝对路径 * @param dstFilePath * 压缩包的绝对路径 * @throws Exception */ public static void zipFile(String srcFilePath, String dstFilePath) throws Exception { ZipOutputStream zipOutputStream = null; BufferedOutputStream bOutputStream = null; try { File srcFile = new File(srcFilePath); if (!srcFile.exists()) {// 源文件不存在 throw new Exception("压缩的源文件不存在!"); } File dstFile = new File(dstFilePath); dstFile.getParentFile().mkdirs();// 创建压缩包所在目录 bOutputStream = new BufferedOutputStream(new FileOutputStream( dstFile)); zipOutputStream = new ZipOutputStream(bOutputStream);// 压缩包输出流 String root = null; if (srcFile.isDirectory()) { root = srcFile.getPath();// 压缩文件夹,根为当前目录 } else { root = srcFile.getParent();// 压缩单个文件,根为父目录 } zipOutputStream = new ZipUtil().zip(root, srcFile, zipOutputStream);// 生成压缩包 } catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } finally { try { if (null != zipOutputStream) { zipOutputStream.close(); } if (null != bOutputStream) { bOutputStream.close(); } } catch (IOException e) { throw e; } } } /** * 将srcFile添加到压缩包zipOutputStream中 * * @param rootPath * 根目录名称 * @param srcFile * 需要压缩的源文件 * @param zipOutputStream * 压缩包输出流 * @throws IOException */ private ZipOutputStream zip(String rootPath, File srcFile, ZipOutputStream zipOutputStream) throws IOException { BufferedInputStream bInputStream = null; try { if (srcFile.isDirectory()) {// 当前为文件夹时 for (File file : srcFile.listFiles()) { new ZipUtil().zip(rootPath, file, zipOutputStream);// 将当前文件夹内所有文件添加到压缩包 } } else { String entryNameString = null;// 当前文件名 if (rootPath.equals(srcFile.getParent())) {// 非子目录下文件 entryNameString = srcFile.getName(); } else {// 子目录下文件 entryNameString = srcFile.getPath(); entryNameString = entryNameString.substring(rootPath .length() + 1, entryNameString.length()); } ZipEntry zipEntry = new ZipEntry(entryNameString);// 压缩包中的文件 zipOutputStream.putNextEntry(zipEntry);// 写入新的文件条目并将流定位到条目数据的开始处 bInputStream = new BufferedInputStream(new FileInputStream( srcFile)); int length = 0; byte[] b = new byte[4096]; while (0 < (length = bInputStream.read(b))) { zipOutputStream.write(b, 0, length); } } return zipOutputStream; } catch (IOException e) { throw e; } finally { if (null != bInputStream) { bInputStream.close(); } } } public static void main(String[] args) throws Exception { ZipUtil .zipFile( "D:/Program Files/Apache Software Foundation/Tomcat 5.5.20/webapps/VoteBaseMul20100401/files/temp/voter/", "D:/Program Files/Apache Software Foundation/Tomcat 5.5.20/webapps/VoteBaseMul20100401/files/temp/voters1.rar"); System.out.println("finished"); } }