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.apache.openejb.maven.plugin.customizer.monkey.jar.JarPatcher.java

private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix)
        throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final ZipArchiveEntry zip = new ZipArchiveEntry(f, path);
    zip.setMethod(method);/*from  w  ww  . j a v a 2s .c  om*/
    final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip);
    jar.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        jar.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                jar(method, jar, child.getCanonicalFile(), prefix);
            }
        }
    } else {
        final InputStream is = new FileInputStream(f);
        IOUtils.copy(is, jar);
        is.close();
        jar.closeArchiveEntry();
    }
}

From source file:org.apache.sis.internal.maven.Assembler.java

/**
 * Adds the given file in the ZIP file. If the given file is a directory, then this method
 * recursively adds all files contained in this directory. This method is invoked for zipping
 * the "application/sis-console/src/main/artifact" directory and sub-directories before to zip
 * the Pack200 file.//w w  w .j ava2 s  . co m
 */
private void appendRecursively(final File file, String relativeFile, final ZipArchiveOutputStream out,
        final byte[] buffer) throws IOException {
    if (file.isDirectory()) {
        relativeFile += '/';
    }
    final ZipArchiveEntry entry = new ZipArchiveEntry(file, relativeFile);
    if (file.canExecute()) {
        entry.setUnixMode(0744);
    }
    out.putArchiveEntry(entry);
    if (!entry.isDirectory()) {
        final FileInputStream in = new FileInputStream(file);
        try {
            int n;
            while ((n = in.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } finally {
            in.close();
        }
    }
    out.closeArchiveEntry();
    if (entry.isDirectory()) {
        for (final String filename : file.list(this)) {
            appendRecursively(new File(file, filename), relativeFile.concat(filename), out, buffer);
        }
    }
}

From source file:org.artifactory.util.ArchiveUtils.java

private static ArchiveEntry createArchiveEntry(File file, String relativePath, ArchiveType archiveType) {
    switch (archiveType) {
    case ZIP:/*  www. jav a 2 s .c o  m*/
        return new ZipArchiveEntry(file, relativePath);
    case TAR:
        return new TarArchiveEntry(file, relativePath);
    case TARGZ:
        return new TarArchiveEntry(file, relativePath);
    case TGZ:
        return new TarArchiveEntry(file, relativePath);
    }

    throw new IllegalArgumentException("Unsupported archive type: '" + archiveType + "'");
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException {
    // Create an entry for the file
    //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString()));
    switch (archivingFormat) {
    case ArchiveStreamFactory.TAR:
        taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;// w ww.jav a2  s.co  m
    case ArchiveStreamFactory.ZIP:
        taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.JAR:
        taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))));
        break;
    case ArchiveStreamFactory.AR:
        taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.CPIO:
        taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        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()) {
            addFilesToArchive(taos, childFile);
        }
    }
}

From source file:org.egov.infra.utils.FileUtils.java

public static Path addFilesToZip(File... files) {
    try {/*from   www  .  j a v  a2  s.  c  om*/
        Path zipFile = Files.createTempFile(UUID.randomUUID().toString(), ZIP_FILE_EXTN);
        try (FileOutputStream zipFileStream = new FileOutputStream(zipFile.toFile());
                ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(zipFileStream)) {
            for (File file : files) {
                zipOutput.putArchiveEntry(new ZipArchiveEntry(file, file.getName()));
                zipOutput.write(IOUtils.toByteArray(file.toURI()));
                zipOutput.closeArchiveEntry();
            }
            return zipFile;
        }
    } catch (IOException e) {
        throw new ApplicationRuntimeException("Error occurred while adding files to ZIP", e);
    }
}

From source file:org.envirocar.app.util.Util.java

/**
 * Android devices up to version 2.3.7 have a bug in the native
 * Zip archive creation, making the archive unreadable with some
 * programs.//from  w ww  . j  a v a2 s  .co  m
 * 
 * @param files
 *            the list of files of the target archive
 * @param target
 *            the target filename
 * @throws IOException
 */
public static void zipInteroperable(List<File> files, String target) throws IOException {
    ZipArchiveOutputStream aos = null;
    OutputStream out = null;
    try {
        File tarFile = new File(target);
        out = new FileOutputStream(tarFile);

        try {
            aos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
                    .createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
        } catch (ArchiveException e) {
            throw new IOException(e);
        }

        for (File file : files) {
            ZipArchiveEntry entry = new ZipArchiveEntry(file, file.getName());
            entry.setSize(file.length());
            aos.putArchiveEntry(entry);
            IOUtils.copy(new FileInputStream(file), aos);
            aos.closeArchiveEntry();
        }

    } catch (IOException e) {
        throw e;
    } finally {
        aos.finish();
        out.close();
    }
}

From source file:org.fabrician.maven.plugins.CompressUtils.java

private static ArchiveEntry createArchiveEntry(File f, String name, OutputStream out, String alternateBaseDir) {
    String substitutedName = substituteAlternateBaseDir(name, f.isDirectory(), alternateBaseDir);
    if (out instanceof TarArchiveOutputStream) {
        return new TarArchiveEntry(f, substitutedName);
    } else {//w  ww  .ja  v a 2 s .co m
        return new ZipArchiveEntry(f, substitutedName);
    }
}

From source file:org.jboss.tools.windup.ui.internal.archiver.ZipFileExporter.java

/**
 * @see org.jboss.tools.windup.ui.internal.archiver.AbstractArchiveFileExporter#getNewArchiveEntry(java.io.File, java.lang.String)
 *//*from   www .j a va  2 s .  co  m*/
@Override
protected ArchiveEntry getNewArchiveEntry(File fileToArchive, String destinationPath) {

    return new ZipArchiveEntry(fileToArchive, destinationPath);
}

From source file:org.owasp.dependencytrack.util.ZipUtil.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
 *
 * @throws IOException If anything goes wrong
 *///  w ww  .  j a  v  a  2  s. co m
private static void addFileToZip(ZipArchiveOutputStream zOut, String path, String base) throws IOException {
    final File f = new File(path);
    final String entryName = base + f.getName();
    final 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();
        final File[] children = f.listFiles();

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

From source file:org.pepstock.jem.util.ZipUtil.java

/** 
 * Creates a zip entry for all files and/or directories of main folder 
 * // w  w w .  ja  va 2 s  .  c  o  m
 * @param zipArchiveOutputStream zip output stream 
 * @param file The file being added 
 * @param path is relative path from main folder 
 * 
 * @throws IOException if any error occurs 
 */
private static void addFileToZip(ZipArchiveOutputStream zipArchiveOutputStream, File file, String path)
        throws IOException {
    // at first call it is the folder, otherwise is the relative path
    String entryName = (path != null) ? path + file.getName() : file.getName();
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, entryName);
    zipArchiveOutputStream.putArchiveEntry(zipEntry);

    // if is a file, add the content to zip file
    if (file.isFile()) {
        FileInputStream fInputStream = null;
        try {
            fInputStream = new FileInputStream(file);
            IOUtils.copy(fInputStream, zipArchiveOutputStream);
            zipArchiveOutputStream.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(fInputStream);
        }
    } else {
        // is a directory so it calls recursively all files in folder
        zipArchiveOutputStream.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToZip(zipArchiveOutputStream, child, entryName + "/");
            }
        }
    }
}