Example usage for org.apache.commons.compress.archivers.zip ZipFile closeQuietly

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

Introduction

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

Prototype

public static void closeQuietly(ZipFile zipfile) 

Source Link

Document

close a zipfile quietly; throw no io fault, do nothing on a null parameter

Usage

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"));
    }/*w  ww  .  jav a2 s.c  om*/
    // 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);
    }
}

From source file:org.jboss.datavirt.commons.dev.server.util.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file/*from ww w  .j a  va  2 s .  c o  m*/
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(
                            "Failed to create parent directory: " + outFile.getParentFile().getCanonicalPath());
                }
            }

            if (entry.isDirectory()) {
                if (!outFile.mkdir()) {
                    throw new IOException("Failed to create directory: " + outFile.getCanonicalPath());
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.overlord.commons.dev.server.util.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file//from   ww w.j  a v a 2s. c o  m
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(
                            "Failed to create parent directory: " + outFile.getParentFile().getCanonicalPath()); //$NON-NLS-1$
                }
            }

            if (entry.isDirectory()) {
                if (!outFile.exists() && !outFile.mkdir()) {
                    throw new IOException("Failed to create directory: " + outFile.getCanonicalPath()); //$NON-NLS-1$
                } else if (outFile.exists() && !outFile.isDirectory()) {
                    throw new IOException("Failed to create directory (already exists but is a file): " //$NON-NLS-1$
                            + outFile.getCanonicalPath());
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.overlord.sramp.atom.archive.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file//from  www  .  jav a2s .  c  o  m
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR", //$NON-NLS-1$
                            outFile.getParentFile().getCanonicalPath()));
                }
            }

            if (entry.isDirectory()) {
                if (!outFile.mkdir()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath())); //$NON-NLS-1$
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.slc.sli.ingestion.landingzone.validation.ZipFileValidator.java

@Override
public boolean isValid(File zipFile, AbstractMessageReport report, ReportStats reportStats, Source source,
        Map<String, Object> parameters) {
    boolean isValid = false;

    // we know more of our source
    LOG.info("Validating zipFile: {}", zipFile.getAbsolutePath());

    ZipFile zf = null;/* w w  w.j  av a  2 s . co  m*/
    try {
        zf = new ZipFile(zipFile);

        Enumeration<ZipArchiveEntry> zes = zf.getEntries();

        while (zes.hasMoreElements()) {
            ZipArchiveEntry ze = zes.nextElement();

            LOG.debug("  ZipArchiveEntry:  name: {}, size {}", ze.getName(), ze.getSize());

            if (isDirectory(ze)) {
                report.error(reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0010,
                        zipFile.getName());
                return false;
            }

            if (ze.getName().endsWith(".ctl")) {
                isValid = true;
            }
        }

        // no manifest (.ctl file) found in the zip file
        if (!isValid) {
            report.error(reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0009,
                    zipFile.getName());
        }
    } catch (UnsupportedZipFeatureException ex) {
        report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0022,
                zipFile.getName());

        isValid = false;
    } catch (FileNotFoundException ex) {
        report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0020,
                zipFile.getName());

        isValid = false;
    } catch (IOException ex) {
        LOG.warn("Caught IO exception processing " + zipFile.getAbsolutePath());

        report.error(ex, reportStats, new FileSource(zipFile.getName()), BaseMessageCode.BASE_0021,
                zipFile.getName());

        isValid = false;
    } finally {
        ZipFile.closeQuietly(zf);
    }

    return isValid;
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Extracts content of the ZIP file to the target folder.
 *
 * @param zipFile   ZIP archive/*from  w  w  w  .  j ava2  s  .  c  om*/
 * @param targetDir Directory to extract files to
 * @param mkdirs    Allow creating missing directories on the file paths
 * @throws IOException           IO Exception
 * @throws FileNotFoundException
 */
public static void extract(File zipFile, File targetDir, boolean mkdirs) throws IOException {
    ZipFile zf = null;

    try {
        zf = new ZipFile(zipFile);

        Enumeration<ZipArchiveEntry> zes = zf.getEntries();

        while (zes.hasMoreElements()) {
            ZipArchiveEntry entry = zes.nextElement();

            if (!entry.isDirectory()) {
                File targetFile = new File(targetDir, entry.getName());

                if (mkdirs) {
                    targetFile.getParentFile().mkdirs();
                }

                InputStream is = null;
                try {
                    is = zf.getInputStream(entry);
                    copyInputStreamToFile(is, targetFile);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Returns a stream for a file stored in the ZIP archive.
 *
 * @param zipFile  ZIP archive//ww w  .j a  v a2 s.  c o m
 * @param fileName Name of the file to get stream for
 * @return Input Stream for the requested file entry
 * @throws IOException           IO Exception
 * @throws FileNotFoundException
 */
public static InputStream getInputStreamForFile(File zipFile, String fileName) throws IOException {
    ZipFile zf = null;
    InputStream fileInputStream = null;

    try {
        zf = new ZipFile(zipFile);

        ZipArchiveEntry entry = zf.getEntry(fileName);

        if (entry == null || entry.isDirectory()) {
            String msg = MessageFormat.format("No file entry is found for {0} withing the {0} archive",
                    fileName, zipFile);
            throw new FileNotFoundException(msg);
        }

        final ZipFile fzf = zf;
        fileInputStream = new BufferedInputStream(zf.getInputStream(entry)) {
            @Override
            public void close() throws IOException {
                super.close();
                ZipFile.closeQuietly(fzf);
            }
        };
    } finally {
        if (fileInputStream == null) {
            ZipFile.closeQuietly(zf);
        }
    }

    return fileInputStream;
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Get the entries in the ZIP file.//from  ww  w.  j a va2 s  . c o m
 *
 * @param zipFileName  ZIP file name
 * @return the entries in the ZIP file
 */
public static Set<String> getZipFileEntries(String zipFileName) throws IOException {
    Enumeration<ZipArchiveEntry> zipFileEntries = null;
    Set<String> filesInZip = new HashSet<String>();

    ZipFile zf = null;

    if (zipFileName == null) {
        return null;
    }

    try {
        zf = new ZipFile(zipFileName);

        zipFileEntries = zf.getEntries();
        while (zipFileEntries.hasMoreElements()) {
            filesInZip.add(zipFileEntries.nextElement().getName());
        }

    } finally {
        if (zf != null) {
            ZipFile.closeQuietly(zf);
        }
    }

    return filesInZip;
}

From source file:org.slc.sli.ingestion.landingzone.ZipFileUtil.java

/**
 * Retrieves a name of the first .ctl file found in the zip archive.
 *
 * @param zipFile ZIP file to scan// w w w .ja  v a2  s . c o  m
 * @return A filename representing the control file.
 * @throws IOException IO Exception
 */
public static String getControlFileName(File zipFile) throws IOException {
    ZipFile zf = null;

    try {
        zf = new ZipFile(zipFile);

        Enumeration<ZipArchiveEntry> zes = zf.getEntries();

        while (zes.hasMoreElements()) {
            ZipArchiveEntry entry = zes.nextElement();

            if (!entry.isDirectory() && entry.getName().endsWith(".ctl")) {
                return entry.getName();
            }
        }
    } finally {
        ZipFile.closeQuietly(zf);
    }

    return null;
}