Here you can find the source of zipDir(File dir, String classPackage, String zipFile)
public static void zipDir(File dir, String classPackage, String zipFile) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Chen Chao(cnfree2000@hotmail.com). * 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 * * Contributors:/* w ww .ja va2 s . c o m*/ * Chen Chao - initial API and implementation *******************************************************************************/ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void zipDir(File dir, String classPackage, String zipFile) throws Exception { File[] files = dir.listFiles(); if (files != null) { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream( zipFile)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) continue; ze = new ZipEntry( (classPackage.length() > 0 ? (classPackage + "/") : "") + file.getName()); //$NON-NLS-1$ //$NON-NLS-2$ ze.setSize(file.length()); ze.setTime(file.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream( new FileInputStream(file)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); } } }