Here you can find the source of zipFiles(String source, String target)
public static void zipFiles(String source, String target) throws Exception
//package com.java2s; // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static final int BUFFER_SIZE = 64 * 1024; public static void zipFiles(String source, String target) throws Exception { zip(source, target);/*from w w w .j a v a 2s .com*/ } /** * zip a new file with specified name to the user folder. * * @param sourceFileName * @param zippedFileName * @throws IOException */ public static void zip(String sourceFileName, String zippedFileName) throws IOException { zip(new File(sourceFileName), zippedFileName, null); } /** * zip a new file with specified name to the user folder. * * @param sourceFileName * @param zippedFileName * @param fileFilter optional * @throws IOException */ public static void zip(String sourceFileName, String zippedFileName, FileFilter fileFilter) throws IOException { zip(new File(sourceFileName), zippedFileName, fileFilter); } /** * zip the file to the user folder. * * @param sourceFile * @param zippedFileName * @throws IOException */ public static void zip(File sourceFile, String zippedFileName) throws IOException { zip(sourceFile, zippedFileName, null); } /** * zip the file to the user folder. * * @param sourceFile * @param zippedFileName * @param fileFilter optional * @throws IOException */ public static void zip(File sourceFile, String zippedFileName, FileFilter fileFilter) throws IOException { if (sourceFile.isDirectory()) { zips(sourceFile.listFiles(fileFilter), zippedFileName, fileFilter); } else { zips(new File[] { sourceFile }, zippedFileName, fileFilter); } } /** * * DOC zshen Comment method "zips". * * @param sourceFile * @param zippedFileName * @param fileFilter optional * @throws IOException */ public static void zips(File[] sourceFile, String zippedFileName) throws IOException { zips(sourceFile, zippedFileName, null); } /** * * DOC zshen Comment method "zips". * * @param sourceFile * @param zippedFileName * @param fileFilter optional * @throws IOException */ public static void zips(File[] sourceFile, String zippedFileName, FileFilter fileFilter) throws IOException { OutputStream fos = new FileOutputStream(zippedFileName); ZipOutputStream out = new ZipOutputStream(fos); try { zips(sourceFile, out, fileFilter); } finally { // http://stackoverflow.com/questions/4681459/closing-zipoutputstream if (sourceFile.length > 0) { out.close(); } else { fos.close(); } } } private static void zips(File[] sourceFile, ZipOutputStream out, FileFilter fileFilter) throws IOException { for (File theFile : sourceFile) { zips(out, theFile, "", fileFilter); //$NON-NLS-1$ } } /** * * DOC zshen Comment method "zip". * * @param out * @param f * @param base * @param fileFilter optional * @throws IOException */ private static void zips(ZipOutputStream out, File f, String base, FileFilter fileFilter) throws IOException { if (f.isDirectory()) { base += f.getName() + '/'; out.putNextEntry(new ZipEntry(base)); for (File element : f.listFiles(fileFilter)) { zips(out, element, base, fileFilter); } } else { out.putNextEntry(new ZipEntry(base + f.getName())); InputStream in = new FileInputStream(f); byte[] b = new byte[BUFFER_SIZE]; int readBytes = 0; while ((readBytes = in.read(b, 0, BUFFER_SIZE)) != -1) { out.write(b, 0, readBytes); } in.close(); out.flush(); } } }