Java Zip Directory zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)

Here you can find the source of zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)

Description

Zips up the directory contents into the specified ZipOutputStream .

License

Open Source License

Parameter

Parameter Description
directory The directory whose contents have to be zipped up
zipout The ZipOutputStream that will be populated by the files found
filter An optional filter that can be used to select only certain files. Can be null, in that case all files in the directory will be zipped

Exception

Parameter Description
IOException an exception
FileNotFoundException an exception

Declaration

public static void zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
        throws IOException, FileNotFoundException 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans/*w w  w. j av a2 s. c o  m*/
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Zips up the directory contents into the specified {@link ZipOutputStream}.
     * <p>
     * Note this method does not take ownership of the provided zip output stream, meaning the
     * client code is responsible for calling {@link ZipOutputStream#finish() finish()} when it's
     * done adding zip entries.
     * </p>
     *
     * @param directory
     *            The directory whose contents have to be zipped up
     * @param zipout
     *            The {@link ZipOutputStream} that will be populated by the files found
     * @param filter
     *            An optional filter that can be used to select only certain files. Can be null, in
     *            that case all files in the directory will be zipped
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void zipDirectory(File directory, ZipOutputStream zipout, final FilenameFilter filter)
            throws IOException, FileNotFoundException {
        zipDirectory(directory, "", zipout, filter);
    }

    /**
     * See {@link #zipDirectory(File, ZipOutputStream, FilenameFilter)}, this version handles the prefix needed
     * to recursively zip data preserving the relative path of each
     */
    private static void zipDirectory(File directory, String prefix, ZipOutputStream zipout,
            final FilenameFilter filter) throws IOException, FileNotFoundException {
        File[] files = directory.listFiles(filter);
        // copy file by reading 4k at a time (faster than buffered reading)
        byte[] buffer = new byte[4 * 1024];
        for (File file : files) {
            if (file.exists()) {
                if (file.isDirectory()) {
                    // recurse and append
                    String newPrefix = prefix + file.getName() + "/";
                    zipout.putNextEntry(new ZipEntry(newPrefix));
                    zipDirectory(file, newPrefix, zipout, filter);
                } else {
                    ZipEntry entry = new ZipEntry(prefix + file.getName());
                    zipout.putNextEntry(entry);

                    InputStream in = new FileInputStream(file);
                    int c;
                    try {
                        while (-1 != (c = in.read(buffer))) {
                            zipout.write(buffer, 0, c);
                        }
                        zipout.closeEntry();
                    } finally {
                        in.close();
                    }
                }
            }
        }
        zipout.flush();
    }
}

Related

  1. zipDirectory(File directory, File zip)
  2. zipDirectory(File directory, File zip)
  3. zipDirectory(File directory, File zipFile)
  4. zipDirectory(File directory, File zipFile)
  5. zipDirectory(File directory, File zipFile, Pattern exclusion)
  6. zipDirectory(File dirToZip, File outputDir, String fileName)
  7. zipDirectory(File folder, File outputFile)
  8. zipDirectory(File inputDirectory, File zipFile)
  9. zipDirectory(File root, File directory, ZipOutputStream zos)