Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream closeArchiveEntry

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream closeArchiveEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream closeArchiveEntry.

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Writes all necessary data for this entry.

Usage

From source file:ThirdParty.zip.java

/**
 * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory
 * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip.
 *
 * @param zOut The zip file's output stream
 * @param path The filesystem path of the file/directory being added
 * @param base The base prefix to for the name of the zip file entry
 * @origin http://developer-tips.hubpages.com/hub/Zipping-and-Unzipping-Nested-Directories-in-Java-using-Apache-Commons-Compress
 * @date 2012-09-05//from w  ww  . jav  a2  s  .c o m
 * @author Robin Spark
 * @throws IOException If anything goes wrong
 */
private static void addFileToZip(ZipArchiveOutputStream zOut, File path, String base) throws IOException {
    File f = path;
    String entryName = base + f.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName);

    zOut.putArchiveEntry(zipEntry);

    if (f.isFile()) {
        FileInputStream fInputStream = null;
        try {
            fInputStream = new FileInputStream(f);
            IOUtils.copy(fInputStream, zOut);
            zOut.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(fInputStream);
        }

    } else {
        zOut.closeArchiveEntry();
        File[] children = f.listFiles();

        if (children != null) {
            for (File child : children) {
                addFileToZip(zOut, child, entryName + "/");
            }
        }
    }
}