Example usage for org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveEntry

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

Introduction

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

Prototype

public ZipArchiveEntry(File inputFile, String entryName) 

Source Link

Document

Creates a new zip entry taking some information from the given file and using the provided name.

Usage

From source file:org.wso2.carbon.connector.util.FileCompressUtil.java

/**
 * Add the files to compression/*ww w. j  av  a  2s  .  com*/
 * 
 * @param taos
 * @param file
 * @param dir
 * @param archiveType
 * @throws IOException
 */
private void addFilesToCompression(ArchiveOutputStream taos, File file, String dir, ArchiveType archiveType)
        throws IOException {

    // Create an entry for the file
    switch (archiveType) {

    case TAR_GZIP:
        taos.putArchiveEntry(new TarArchiveEntry(file, dir + "/" + file.getName()));
        break;

    case ZIP:
        taos.putArchiveEntry(new ZipArchiveEntry(file, dir + "/" + file.getName()));
        break;
    }

    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();

    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion,
        // add them to the archive

        for (File childFile : file.listFiles()) {
            addFilesToCompression(taos, childFile, file.getName(), archiveType);
        }
    }

}

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.j  a va2 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 + "/");
            }
        }
    }
}