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

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

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Is this entry a directory?

Usage

From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java

protected void execute() throws ArchiverException {
    getLogger().debug("Expanding: " + getSourceFile() + " into " + getDestDirectory());
    org.apache.commons.compress.archivers.zip.ZipFile zf = null;
    try {// w w w.  j a va  2s  . c o  m
        zf = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);
        final Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zf, ze);
            if (isSelected(ze.getName(), fileInfo)) {
                InputStream in = zf.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zf, ze));
                IOUtil.close(in);
            }

        }

        getLogger().debug("expand complete");
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zf);
    }
}

From source file:org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver.java

protected void execute(final String path, final File outputDirectory) throws ArchiverException {
    org.apache.commons.compress.archivers.zip.ZipFile zipFile = null;

    try {/*  ww  w.  j a v a2  s . c om*/
        zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(getSourceFile(), encoding);

        final Enumeration e = zipFile.getEntries();

        while (e.hasMoreElements()) {
            final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo(zipFile, ze);
            if (!isSelected(ze.getName(), fileInfo)) {
                continue;
            }

            if (ze.getName().startsWith(path)) {
                final InputStream inputStream = zipFile.getInputStream(ze);
                extractFileIfIncluded(getSourceFile(), outputDirectory, inputStream, ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(),
                        ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink(zipFile, ze));
                IOUtil.close(inputStream);
            }
        }
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

From source file:org.codehaus.plexus.archiver.zip.ConcurrentJarCreator.java

/**
 * Adds an archive entry to this archive.
 * <p/>//from  ww  w .  j  av a  2 s  . c  o m
 * This method is expected to be called from a single client thread
 *
 * @param zipArchiveEntry The entry to add. Compression method
 * @param source          The source input stream supplier
 * @throws java.io.IOException
 */

public void addArchiveEntry(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier source)
        throws IOException {
    final int method = zipArchiveEntry.getMethod();
    if (method == -1)
        throw new IllegalArgumentException("Method must be set on the supplied zipArchiveEntry");
    if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink()) {
        final ByteArrayInputStream payload = new ByteArrayInputStream(new byte[] {});
        directories.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else if ("META-INF".equals(zipArchiveEntry.getName())) {
        InputStream payload = source.get();
        if (zipArchiveEntry.isDirectory())
            zipArchiveEntry.setMethod(ZipEntry.STORED);
        metaInfDir.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else if ("META-INF/MANIFEST.MF".equals(zipArchiveEntry.getName())) {
        InputStream payload = source.get();
        if (zipArchiveEntry.isDirectory())
            zipArchiveEntry.setMethod(ZipEntry.STORED);
        manifest.addArchiveEntry(
                createZipArchiveEntryRequest(zipArchiveEntry, createInputStreamSupplier(payload)));
        payload.close();
    } else {
        parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, source);
    }
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

private void createArchive(ZipArchiver archiver) throws ArchiverException, IOException {
    archiver.createArchive();/* w  ww  .  j a  v  a2  s  . com*/

    ZipFile zf = new ZipFile(archiver.getDestFile());

    Enumeration e = zf.getEntries();

    while (e.hasMoreElements()) {
        ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
        if (ze.isDirectory()) {
            if (ze.getName().startsWith("worldwritable")) {
                fileModeAssert(0777, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.getName().startsWith("groupwritable")) {
                fileModeAssert(0070, UnixStat.PERM_MASK & ze.getUnixMode());
            } else {
                fileModeAssert(0500, UnixStat.PERM_MASK & ze.getUnixMode());
            }
        } else {
            if (ze.getName().equals("one.txt")) {
                fileModeAssert(0640, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.getName().equals("two.txt")) {
                fileModeAssert(0664, UnixStat.PERM_MASK & ze.getUnixMode());
            } else if (ze.isUnixSymlink()) {
                //         assertEquals( ze.getName(), 0500, UnixStat.PERM_MASK & ze.getUnixMode() );
            } else {
                fileModeAssert(0400, UnixStat.PERM_MASK & ze.getUnixMode());
            }
        }

    }
}

From source file:org.dbflute.helper.io.compress.DfZipArchiver.java

/**
 * Extract the archive file to the directory.
 * @param baseDir The base directory to compress. (NotNull)
 * @param filter The file filter, which doesn't need to accept the base directory. (NotNull)
 *///www  . j a  v  a2 s . com
public void extract(File baseDir, FileFilter filter) {
    if (baseDir == null) {
        String msg = "The argument 'baseDir' should not be null.";
        throw new IllegalArgumentException(msg);
    }
    if (baseDir.exists() && !baseDir.isDirectory()) {
        String msg = "The baseDir should be directory but not: " + baseDir;
        throw new IllegalArgumentException(msg);
    }
    baseDir.mkdirs();
    final String baseDirPath = resolvePath(baseDir);
    InputStream ins = null;
    ZipArchiveInputStream archive = null;
    try {
        ins = new FileInputStream(_zipFile);
        archive = new ZipArchiveInputStream(ins, "UTF-8", true);
        ZipArchiveEntry entry;
        while ((entry = archive.getNextZipEntry()) != null) {
            final String entryName = resolveFileSeparator(entry.getName()); // just in case
            final File file = new File(baseDirPath + "/" + entryName);
            if (!filter.accept(file)) {
                continue;
            }
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                OutputStream out = null;
                try {
                    out = new FileOutputStream(file);
                    IOUtils.copy(archive, out);
                    out.close();
                } catch (IOException e) {
                    String msg = "Failed to IO-copy the file: " + file.getPath();
                    throw new IllegalStateException(msg, e);
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException ignored) {
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        String msg = "Failed to extract the files from " + _zipFile.getPath();
        throw new IllegalArgumentException(msg, e);
    } finally {
        if (archive != null) {
            try {
                archive.close();
            } catch (IOException ignored) {
            }
        }
        if (ins != null) {
            try {
                ins.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:org.eclipse.cbi.maven.plugins.macsigner.SignMojo.java

/**
 * Decompresses zip files.//from w w  w.j av  a  2s. co m
 * @param zipFile           The zip file to decompress.
 * @throws IOException
 * @throws MojoExecutionException
 */
private static void unZip(File zipFile, File output_dir) throws IOException, MojoExecutionException {

    ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFile));
    ZipArchiveEntry ze;
    String name, parent;
    try {
        ze = zis.getNextZipEntry();
        // check for at least one zip entry
        if (ze == null) {
            throw new MojoExecutionException("Could not decompress " + zipFile);
        }

        while (ze != null) {
            name = ze.getName();

            //make directories
            if (ze.isDirectory()) {
                mkdirs(output_dir, name);
            } else {
                parent = getParentDirAbsolutePath(name);
                mkdirs(output_dir, parent);

                File outFile = new File(output_dir, name);
                outFile.createNewFile();

                // check for match in executable list
                if (executableFiles.contains(name)) {
                    Files.setPosixFilePermissions(outFile.toPath(),
                            PosixFilePermissions.fromString("rwxr-x---"));
                }

                FileOutputStream fos = new FileOutputStream(outFile);

                copyInputStreamToOutputStream(zis, fos);
                fos.close();
            }
            ze = zis.getNextZipEntry();
        }
    } finally {
        zis.close();
    }
}

From source file:org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ZipLeveledStructureProvider.java

/**
 * Initializes this object's children table based on the contents of the
 * specified source file./*w  w  w  .  j  a  va 2  s. c  om*/
 */
protected void initialize() {
    children = new HashMap<>(1000);

    children.put(root, new ArrayList<>());
    Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        IPath path = new Path(entry.getName()).addTrailingSeparator();

        if (entry.isDirectory()) {
            createContainer(path);
        } else {
            // Ensure the container structure for all levels above this is initialized
            // Once we hit a higher-level container that's already added we need go no further
            int pathSegmentCount = path.segmentCount();
            if (pathSegmentCount > 1) {
                createContainer(path.uptoSegment(pathSegmentCount - 1));
            }
            createFile(entry);
        }
    }
}

From source file:org.everit.osgi.dev.maven.util.FileManager.java

public final void unpackZipFile(final File file, final File destinationDirectory) throws IOException {
    destinationDirectory.mkdirs();// w  w  w .  j a  v  a  2s  .  c  o  m
    ZipFile zipFile = new ZipFile(file);

    try {
        Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            String name = entry.getName();
            File destFile = new File(destinationDirectory, name);
            if (entry.isDirectory()) {
                destFile.mkdirs();
            } else {
                File parentFolder = destFile.getParentFile();
                parentFolder.mkdirs();
                InputStream inputStream = zipFile.getInputStream(entry);
                overCopyFile(inputStream, destFile);
                FileManager.setPermissionsOnFile(destFile, entry);
            }

        }
    } finally {
        zipFile.close();
    }
}

From source file:org.interreg.docexplore.util.ZipUtils.java

public static void unzip(File zipfile, File directory, float[] progress, float progressOffset,
        float progressAmount, boolean overwrite) throws IOException {
    org.apache.commons.compress.archivers.zip.ZipFile zfile = new org.apache.commons.compress.archivers.zip.ZipFile(
            zipfile);//from  w ww  .j  a v  a2 s . co m

    int nEntries = 0;
    Enumeration<ZipArchiveEntry> entries = zfile.getEntries();
    while (entries.hasMoreElements())
        if (!entries.nextElement().isDirectory())
            nEntries++;

    int cnt = 0;
    entries = zfile.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        File file = new File(directory, entry.getName());
        if (entry.isDirectory())
            file.mkdirs();
        else {
            if (!file.exists() || overwrite) {
                file.getParentFile().mkdirs();
                InputStream in = zfile.getInputStream(entry);
                try {
                    copy(in, file);
                } finally {
                    in.close();
                }
            }

            cnt++;
            if (progress != null)
                progress[0] = progressOffset + cnt * progressAmount / nEntries;
        }
    }
    zfile.close();
}

From source file:org.jboss.as.forge.util.Files.java

public static boolean extractAppServer(final String zipPath, final File target, final boolean overwrite)
        throws IOException {
    if (target.exists() && !overwrite) {
        throw new IllegalStateException(Messages.INSTANCE.getMessage("files.not.empty.directory"));
    }/*from   w  w  w .ja  v a 2  s  . c  o  m*/
    // Create a temporary directory
    final File tmpDir = new File(getTempDirectory(), "jboss-as-" + zipPath.hashCode());
    if (tmpDir.exists()) {
        deleteRecursively(tmpDir);
    }
    try {
        final byte buff[] = new byte[1024];
        ZipFile file = null;
        try {
            file = new ZipFile(zipPath);
            final Enumeration<ZipArchiveEntry> entries = file.getEntries();
            while (entries.hasMoreElements()) {
                final ZipArchiveEntry entry = entries.nextElement();
                // Create the extraction target
                final File extractTarget = new File(tmpDir, entry.getName());
                if (entry.isDirectory()) {
                    extractTarget.mkdirs();
                } else {
                    final File parent = new File(extractTarget.getParent());
                    parent.mkdirs();
                    final BufferedInputStream in = new BufferedInputStream(file.getInputStream(entry));
                    try {
                        final BufferedOutputStream out = new BufferedOutputStream(
                                new FileOutputStream(extractTarget));
                        try {
                            int read;
                            while ((read = in.read(buff)) != -1) {
                                out.write(buff, 0, read);
                            }
                        } finally {
                            Streams.safeClose(out);
                        }
                    } finally {
                        Streams.safeClose(in);
                    }
                    // Set the file permissions
                    if (entry.getUnixMode() > 0) {
                        setPermissions(extractTarget, FilePermissions.of(entry.getUnixMode()));
                    }
                }
            }
        } catch (IOException e) {
            throw new IOException(Messages.INSTANCE.getMessage("files.extraction.error", file), e);
        } finally {
            ZipFile.closeQuietly(file);
            // Streams.safeClose(file);
        }
        // If the target exists, remove then rename
        if (target.exists()) {
            deleteRecursively(target);
        }
        // First child should be a directory and there should only be one child
        final File[] children = tmpDir.listFiles();
        if (children != null && children.length == 1) {
            return moveDirectory(children[0], target);
        }
        return moveDirectory(tmpDir, target);

    } finally {
        deleteRecursively(tmpDir);
    }
}