Here you can find the source of addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
private static void addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip) throws IOException
//package com.java2s; /*####################################################### * * Maintained by Gregor Santner, 2017- * https://gsantner.net///from w ww.j ava 2 s . co m * * License: Apache 2.0 * https://github.com/gsantner/opoc/#licensing * https://www.apache.org/licenses/LICENSE-2.0 * #########################################################*/ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { private static final int BUFFER_SIZE = 4096; private static void addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip) throws IOException { pathInsideZip = pathInsideZip.isEmpty() ? folderToZip.getName() : pathInsideZip + "/" + folderToZip.getName(); File[] files = folderToZip.listFiles(); if (files != null) { for (File file : files) addFileToZip(pathInsideZip, file, outZip); } } private static void addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip) throws IOException { if (fileToZip.isDirectory()) { addFolderToZip(pathInsideZip, fileToZip, outZip); } else { FileInputStream in = null; try { in = new FileInputStream(fileToZip); outZip.putNextEntry(new ZipEntry(pathInsideZip + "/" + fileToZip.getName())); int count; byte[] buffer = new byte[BUFFER_SIZE]; while ((count = in.read(buffer)) > 0) outZip.write(buffer, 0, count); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { } } } } } }