Here you can find the source of zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList
private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList<String> avoidingFiles) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList<String> avoidingFiles) throws IOException { try {/*from w w w . j av a 2 s. c o m*/ System.out.println(directory.getAbsolutePath() + "::" + zipPath); // get a listing of the directory content String[] dirList = directory.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(directory, dirList[i]); System.err.println("avoiding: " + avoidingFiles); System.err.println("file: " + f.getName()); System.err.println("and so: " + avoidingFiles.contains(f.getName())); if (!avoidingFiles.contains(f.getName())) { if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively // String filePath = f.getPath(); // File file = new File(zipPath+"/"+f.getName()); zipDirectoryRecursive(new File(f.getPath()), zipPath + "/" + f.getName(), zos, avoidingFiles); 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 zip entry // ZipEntry anEntry = new ZipEntry(f.getPath()); ZipEntry anEntry = new ZipEntry(zipPath + "/" + f.getName()); // place the zip entry in the ZipOutputStream object zos.putNextEntry(anEntry); // now write the content of the file to the ZipOutputStream while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } fis.close(); } } } catch (Exception e) { e.printStackTrace(); } } private static void zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos) throws IOException { try { System.out.println(directory.getAbsolutePath() + "::" + zipPath); // get a listing of the directory content String[] dirList = directory.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(directory, dirList[i]); if (f.isDirectory()) { // if the File object is a directory, call this // function again to add its content recursively // String filePath = f.getPath(); // File file = new File(zipPath+"/"+f.getName()); zipDirectoryRecursive(new File(f.getPath()), zipPath + "/" + f.getName(), zos); 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 zip entry // ZipEntry anEntry = new ZipEntry(f.getPath()); ZipEntry anEntry = new ZipEntry(zipPath + "/" + f.getName()); // place the zip entry in the ZipOutputStream object zos.putNextEntry(anEntry); // now write the content of the file to the ZipOutputStream while ((bytesIn = fis.read(readBuffer)) != -1) { zos.write(readBuffer, 0, bytesIn); } fis.close(); } } catch (Exception e) { e.printStackTrace(); } } }