Here you can find the source of zipDir(File directory, ZipOutputStream zos, String path, Set
public static void zipDir(File directory, ZipOutputStream zos, String path, Set<String> exclusions) throws IOException
//package com.java2s; /*//from w w w. jav a 2 s.c om * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** * Zip up a directory path */ public static void zipDir(File directory, ZipOutputStream zos, String path, Set<String> exclusions) throws IOException { // get a listing of the directory content File[] dirList = directory.listFiles(); byte[] readBuffer = new byte[8192]; int bytesIn; // loop through dirList, and zip the files assert dirList != null; for (File f : dirList) { if (f.isDirectory()) { String prefix = path + f.getName() + "/"; zos.putNextEntry(new ZipEntry(prefix)); zipDir(f, zos, prefix, exclusions); continue; } String entry = path + f.getName(); if (!exclusions.contains(entry)) { FileInputStream fis = new FileInputStream(f); try { ZipEntry anEntry = new ZipEntry(entry); zos.putNextEntry(anEntry); bytesIn = fis.read(readBuffer); while (bytesIn != -1) { zos.write(readBuffer, 0, bytesIn); bytesIn = fis.read(readBuffer); } } finally { fis.close(); } } } } }