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

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

Introduction

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

Prototype

public InputStream getInputStream(ZipArchiveEntry ze) throws IOException, ZipException 

Source Link

Document

Returns an InputStream for reading the contents of the given entry.

Usage

From source file:ee.sk.digidoc.factory.SAXDigiDocFactory.java

/**
 * Reads in a DigiDoc file. One of fname or isSdoc must be given.
 * @param fname signed doc filename//from w  ww  .  j a  v  a2s  .co m
 * @param isSdoc opened stream with DigiDoc data
 * The user must open and close it.
 * @param errs list of errors to fill with parsing errors. If given
 * then attempt is made to continue parsing on errors and return them in this list.
 * If not given (null) then the first error found will be thrown.
 * @return signed document object if successfully parsed
 */
private SignedDoc readSignedDocOfType(String fname, InputStream isSdoc, boolean isBdoc, List errs)
        throws DigiDocException {
    // Use an instance of ourselves as the SAX event handler
    SAXDigiDocFactory handler = this;
    m_errs = errs;
    DigiDocVerifyFactory.initProvider();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    if (m_logger.isDebugEnabled())
        m_logger.debug("Start reading ddoc/bdoc " + ((fname != null) ? "from file: " + fname : "from stream")
                + " bdoc: " + isBdoc);
    if (fname == null && isSdoc == null) {
        throw new DigiDocException(DigiDocException.ERR_READ_FILE, "No input file", null);
    }
    if (fname != null) {
        File inFile = new File(fname);
        if (!inFile.canRead() || inFile.length() == 0) {
            throw new DigiDocException(DigiDocException.ERR_READ_FILE, "Empty or unreadable input file", null);
        }
    }
    ZipFile zf = null;
    ZipArchiveInputStream zis = null;
    ZipArchiveEntry ze = null;
    InputStream isEntry = null;
    File fTmp = null;
    try {
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        if (isBdoc) { // bdoc parsing
            // must be a bdoc document ?
            m_doc = new SignedDoc();
            m_doc.setVersion(SignedDoc.BDOC_VERSION_1_0);
            m_doc.setFormat(SignedDoc.FORMAT_BDOC);
            Enumeration eFiles = null;
            if (fname != null) {
                zf = new ZipFile(fname, "UTF-8");
                eFiles = zf.getEntries();
            } else if (isSdoc != null) {
                zis = new ZipArchiveInputStream(isSdoc, "UTF-8", true, true);
            }
            ArrayList lSigFnames = new ArrayList();
            ArrayList lDataFnames = new ArrayList();
            // read all entries
            boolean bHasMimetype = false, bManifest1 = false;
            int nFil = 0;
            while ((zf != null && eFiles.hasMoreElements())
                    || (zis != null && ((ze = zis.getNextZipEntry()) != null))) {
                nFil++;

                // read entry
                if (zf != null) { // ZipFile
                    ze = (ZipArchiveEntry) eFiles.nextElement();
                    isEntry = zf.getInputStream(ze);
                } else { // ZipArchiveInputStream
                    int n = 0, nTot = 0;
                    if ((ze.getName().equals(FILE_MIMETYPE) || ze.getName().equals(FILE_MANIFEST)
                            || (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")))
                            || (nMaxBdocFilCached <= 0
                                    || (ze.getSize() < nMaxBdocFilCached && ze.getSize() >= 0))) {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            bos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip");
                        data = bos.toByteArray();
                        bos = null;
                        isEntry = new ByteArrayInputStream(data);
                    } else {
                        File fCacheDir = new File(ConfigManager.instance().getStringProperty(
                                "DIGIDOC_DF_CACHE_DIR", System.getProperty("java.io.tmpdir")));
                        fTmp = File.createTempFile("bdoc-data", ".tmp", fCacheDir);
                        FileOutputStream fos = new FileOutputStream(fTmp);
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            fos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip to: " + fTmp.getAbsolutePath());
                        fos.close();
                        isEntry = new FileInputStream(fTmp);
                    }
                }
                if (m_logger.isDebugEnabled())
                    m_logger.debug("Entry: " + ze.getName() + " nlen: " + ze.getName().length() + " size: "
                            + ze.getSize() + " dir: " + ze.isDirectory() + " comp-size: "
                            + ze.getCompressedSize());
                // mimetype file
                if (ze.getName().equals(FILE_MIMETYPE)) {
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Check mimetype!");
                    checkBdocMimetype(isEntry);
                    bHasMimetype = true;
                    m_doc.setComment(ze.getComment());
                    if (nFil != 1) {
                        m_logger.error("mimetype file is " + nFil + " file but must be first");
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "mimetype file is not first zip entry", null));
                    }
                } else if (ze.getName().equals(FILE_MANIFEST)) { // manifest.xml file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read manifest");
                    if (!bManifest1 && isEntry != null) {
                        bManifest1 = true;
                        BdocManifestParser mfparser = new BdocManifestParser(m_doc);
                        mfparser.readManifest(isEntry);
                    } else {
                        m_logger.error("Found multiple manifest.xml files!");
                        throw new DigiDocException(DigiDocException.ERR_MULTIPLE_MANIFEST_FILES,
                                "Found multiple manifest.xml files!", null);
                    }
                } else if (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")) { // some signature
                    m_fileName = ze.getName();
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Reading bdoc siganture: " + m_fileName);
                    boolean bExists = false;
                    for (int j = 0; j < lSigFnames.size(); j++) {
                        String s1 = (String) lSigFnames.get(j);
                        if (s1.equals(m_fileName))
                            bExists = true;
                    }
                    if (bExists) {
                        m_logger.error("Duplicate signature filename: " + m_fileName);
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "Duplicate signature filename: " + m_fileName, null));
                    } else
                        lSigFnames.add(m_fileName);
                    SAXParser saxParser = factory.newSAXParser();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int n = 0;
                    byte[] data = new byte[2048];
                    while ((n = isEntry.read(data)) > 0)
                        bos.write(data, 0, n);
                    data = bos.toByteArray();
                    bos = null;
                    if (m_logger.isDebugEnabled())
                        m_logger.debug(
                                "Parsing bdoc: " + m_fileName + " size: " + ((data != null) ? data.length : 0));
                    saxParser.parse(new SignatureInputStream(new ByteArrayInputStream(data)), this);
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Parsed bdoc: " + m_fileName);
                    Signature sig1 = m_doc.getLastSignature();
                    m_sigComment = ze.getComment();
                    if (sig1 != null) {
                        sig1.setPath(m_fileName);
                        sig1.setComment(ze.getComment());
                    }
                } else { // probably a data file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read data file: " + ze.getName());
                    if (!ze.isDirectory()) {
                        boolean bExists = false;
                        for (int j = 0; j < lDataFnames.size(); j++) {
                            String s1 = (String) lDataFnames.get(j);
                            if (s1.equals(ze.getName()))
                                bExists = true;
                        }
                        if (bExists) {
                            m_logger.error("Duplicate datafile filename: " + ze.getName());
                            handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                    "Duplicate datafile filename: " + ze.getName(), null));
                        } else
                            lDataFnames.add(ze.getName());
                        DataFile df = m_doc.findDataFileById(ze.getName());
                        if (df != null) {
                            if (ze.getSize() > 0)
                                df.setSize(ze.getSize());
                            df.setContentType(DataFile.CONTENT_BINARY);
                            df.setFileName(ze.getName());
                        } else {
                            df = new DataFile(ze.getName(), DataFile.CONTENT_BINARY, ze.getName(),
                                    "application/binary", m_doc);
                            if (m_doc.getDataFiles() == null)
                                m_doc.setDataFiles(new ArrayList());
                            m_doc.getDataFiles().add(df);
                            //m_doc.addDataFile(df); // this does some intiailization work unnecessary here
                        }
                        // enable caching if requested
                        if (isEntry != null)
                            df.setOrCacheBodyAndCalcHashes(isEntry);
                        df.setComment(ze.getComment());
                        df.setLastModDt(new Date(ze.getTime()));
                        // fix mime type according to DataObjectFormat
                        Signature sig1 = m_doc.getLastSignature();
                        if (sig1 != null) {
                            Reference dRef = sig1.getSignedInfo().getReferenceForDataFile(df);
                            if (dRef != null) {
                                DataObjectFormat dof = sig1.getSignedInfo()
                                        .getDataObjectFormatForReference(dRef);
                                if (dof != null) {
                                    df.setMimeType(dof.getMimeType());
                                }
                            }
                        }
                    }
                }
                if (fTmp != null) {
                    fTmp.delete();
                    fTmp = null;
                }
            } // while zip entries
            if (!bHasMimetype) {
                m_logger.error("No mimetype file");
                handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                        "Not a BDOC format file! No mimetype file!", null));
            }
            // if no signatures exist then copy mime-type from manifest.xml to DataFile -s
            if (m_doc.countSignatures() == 0) {
                for (int i = 0; i < m_doc.countDataFiles(); i++) {
                    DataFile df = m_doc.getDataFile(i);
                    if (m_doc.getManifest() != null) {
                        for (int j = 0; j < m_doc.getManifest().getNumFileEntries(); j++) {
                            ManifestFileEntry mfe = m_doc.getManifest().getFileEntry(j);
                            if (mfe.getFullPath() != null && mfe.getFullPath().equals(df.getFileName())) {
                                df.setMimeType(mfe.getMediaType());
                            } // if fullpath
                        } // for
                    } // if
                } // for i
            }
        } else { // ddoc parsing
            if (m_logger.isDebugEnabled())
                m_logger.debug("Reading ddoc: " + fname + " file: " + m_fileName);
            m_fileName = fname;
            SAXParser saxParser = factory.newSAXParser();
            if (fname != null)
                saxParser.parse(new SignatureInputStream(new FileInputStream(fname)), this);
            else if (isSdoc != null)
                saxParser.parse(isSdoc, this);
        }
    } catch (org.xml.sax.SAXParseException ex) {
        m_logger.error("SAX Error: " + ex);
        handleError(ex);

    } catch (Exception ex) {
        m_logger.error("Error reading3: " + ex);
        ex.printStackTrace();
        /*if(ex instanceof DigiDocException){
           DigiDocException dex = (DigiDocException)ex;
           m_logger.error("Dex: " + ex);
           if(dex.getNestedException() != null) {
              dex.getNestedException().printStackTrace();
              m_logger.error("Trace: "); 
           }
        }*/
        handleError(ex);
    } finally { // cleanup
        try {
            if (isEntry != null) {
                isEntry.close();
                isEntry = null;
            }
            if (zis != null)
                zis.close();
            if (zf != null)
                zf.close();
            if (fTmp != null) {
                fTmp.delete();
                fTmp = null;
            }
        } catch (Exception ex) {
            m_logger.error("Error closing streams and files: " + ex);
        }
    }
    // compare Manifest and DataFiles
    boolean bErrList = (errs != null);
    if (errs == null)
        errs = new ArrayList();
    boolean bOk = DigiDocVerifyFactory.verifyManifestEntries(m_doc, errs);
    if (m_doc == null) {
        m_logger.error("Error reading4: doc == null");
        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                "This document is not in ddoc or bdoc format", null));
    }
    if (!bErrList && errs.size() > 0) { // if error list was not used then we have to throw exception. So we will throw the first one since we can only do it once
        DigiDocException ex = (DigiDocException) errs.get(0);
        throw ex;
    }
    return m_doc;
}

From source file:com.android.tools.idea.sdk.remote.internal.archives.ArchiveInstaller.java

/**
 * Unzips a zip file into the given destination directory.
 * <p/>//from  w  ww.  jav a2s  . c  om
 * The archive file MUST have a unique "root" folder.
 * This root folder is skipped when unarchiving.
 */
@SuppressWarnings("unchecked")
@VisibleForTesting(visibility = Visibility.PRIVATE)
protected boolean unzipFolder(ArchiveReplacement archiveInfo, File archiveFile, File unzipDestFolder,
        ITaskMonitor monitor) {

    Archive newArchive = archiveInfo.getNewArchive();
    RemotePkgInfo pkg = newArchive.getParentPackage();
    String pkgName = pkg.getShortDescription();
    long compressedSize = newArchive.getSize();

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory (e.g. docs/index.html).
            int pos = name.indexOf('/');
            if (pos == -1) {
                // All zip entries should have a root folder.
                // This zip entry seems located at the root of the zip.
                // Rather than ignore the file, just place it at the root.
            } else if (pos == name.length() - 1) {
                // This is a zip *directory* entry in the form dir/, so essentially
                // it's the root directory of the SDK. It's safe to ignore that one
                // since we want to use our own root directory and we'll recreate
                // root directories as needed.
                // A direct consequence is that if a malformed archive has multiple
                // root directories, their content will all be merged together.
                continue;
            } else {
                // This is the expected behavior: the zip entry is in the form root/file
                // or root/dir/. We want to use our top-level directory so we drop the
                // first segment of the path name.
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!mFileOp.isDirectory(destFile) && !mFileOp.mkdirs(destFile)) {
                    monitor.logError("Failed to create directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.

                // Sanity check: since we're always unzipping in a fresh temp folder
                // the destination file shouldn't already exist.
                if (mFileOp.exists(destFile)) {
                    monitor.logVerbose("Duplicate file found:  %1$s", name);
                }

                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!mFileOp.isDirectory(parentDir)) {
                    if (!mFileOp.mkdirs(parentDir)) {
                        monitor.logError("Failed to create directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            long remains = entry.getSize();
            try {
                fos = new FileOutputStream(destFile);

                // Java bug 4040920: do not rely on the input stream EOF and don't
                // try to read more than the entry's size.
                InputStream entryContent = zipFile.getInputStream(entry);
                int n;
                while (remains > 0
                        && (n = entryContent.read(buf, 0, (int) Math.min(remains, buf.length))) != -1) {
                    remains -= n;
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } catch (EOFException e) {
                monitor.logError("Error uncompressing file %s. Size: %d bytes, Unwritten: %d bytes.",
                        entry.getName(), entry.getSize(), remains);
                throw e;
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            pkg.postUnzipFileHook(newArchive, monitor, mFileOp, destFile, entry);

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription("Unzipping %1$s (%2$d%%)", pkgName, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.logError("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:com.android.sdklib.internal.repository.archives.ArchiveInstaller.java

/**
 * Unzips a zip file into the given destination directory.
 *
 * The archive file MUST have a unique "root" folder.
 * This root folder is skipped when unarchiving.
 */// ww  w. java 2  s. c  o  m
@SuppressWarnings("unchecked")
@VisibleForTesting(visibility = Visibility.PRIVATE)
protected boolean unzipFolder(ArchiveReplacement archiveInfo, File archiveFile, File unzipDestFolder,
        ITaskMonitor monitor) {

    Archive newArchive = archiveInfo.getNewArchive();
    Package pkg = newArchive.getParentPackage();
    String pkgName = pkg.getShortDescription();
    long compressedSize = newArchive.getSize();

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory (e.g. docs/index.html).
            int pos = name.indexOf('/');
            if (pos == -1) {
                // All zip entries should have a root folder.
                // This zip entry seems located at the root of the zip.
                // Rather than ignore the file, just place it at the root.
            } else if (pos == name.length() - 1) {
                // This is a zip *directory* entry in the form dir/, so essentially
                // it's the root directory of the SDK. It's safe to ignore that one
                // since we want to use our own root directory and we'll recreate
                // root directories as needed.
                // A direct consequence is that if a malformed archive has multiple
                // root directories, their content will all be merged together.
                continue;
            } else {
                // This is the expected behavior: the zip entry is in the form root/file
                // or root/dir/. We want to use our top-level directory so we drop the
                // first segment of the path name.
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!mFileOp.isDirectory(destFile) && !mFileOp.mkdirs(destFile)) {
                    monitor.logError("Failed to create directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.

                // Sanity check: since we're always unzipping in a fresh temp folder
                // the destination file shouldn't already exist.
                if (mFileOp.exists(destFile)) {
                    monitor.logVerbose("Duplicate file found:  %1$s", name);
                }

                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!mFileOp.isDirectory(parentDir)) {
                    if (!mFileOp.mkdirs(parentDir)) {
                        monitor.logError("Failed to create directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            long remains = entry.getSize();
            try {
                fos = new FileOutputStream(destFile);

                // Java bug 4040920: do not rely on the input stream EOF and don't
                // try to read more than the entry's size.
                InputStream entryContent = zipFile.getInputStream(entry);
                int n;
                while (remains > 0
                        && (n = entryContent.read(buf, 0, (int) Math.min(remains, buf.length))) != -1) {
                    remains -= n;
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } catch (EOFException e) {
                monitor.logError("Error uncompressing file %s. Size: %d bytes, Unwritten: %d bytes.",
                        entry.getName(), entry.getSize(), remains);
                throw e;
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            pkg.postUnzipFileHook(newArchive, monitor, mFileOp, destFile, entry);

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription("Unzipping %1$s (%2$d%%)", pkgName, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.logError("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:net.sourceforge.pmd.it.ZipFileExtractor.java

/**
 * Extracts the given zip file into the tempDir.
 * @param zipPath the zip file to extract
 * @param tempDir the target directory//from  w  ww .j a v a2 s.c o m
 * @throws Exception if any error happens during extraction
 */
public static void extractZipFile(Path zipPath, Path tempDir) throws Exception {
    ZipFile zip = new ZipFile(zipPath.toFile());
    try {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            File file = tempDir.resolve(entry.getName()).toFile();
            if (entry.isDirectory()) {
                assertTrue(file.mkdirs());
            } else {
                try (InputStream data = zip.getInputStream(entry);
                        OutputStream fileOut = new FileOutputStream(file);) {
                    IOUtils.copy(data, fileOut);
                }
                if ((entry.getUnixMode() & OWNER_EXECUTABLE) == OWNER_EXECUTABLE) {
                    file.setExecutable(true);
                }
            }
        }
    } finally {
        zip.close();
    }
}

From source file:org.abysm.onionzip.ZipFileExtractHelper.java

void extractZipArchiveEntries() throws IOException {
    ZipFile zipFile = new ZipFile(zipFilename, encoding);
    try {//from  w w  w  .  j  a  v  a 2 s. c om
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            System.out.println(entry.getName());
            if (entry.isDirectory()) {
                Path directory = Paths.get(entry.getName());
                Files.createDirectories(directory);
            } else if (entry.isUnixSymlink()) {
                Path symlink = Paths.get(entry.getName());
                Path parentDirectory = symlink.getParent();
                Path target = Paths.get(zipFile.getUnixSymlink(entry));
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                Files.createSymbolicLink(symlink, target);
            } else {
                Path file = Paths.get(entry.getName());
                Path parentDirectory = file.getParent();
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                InputStream contentInputStream = zipFile.getInputStream(entry);
                FileOutputStream extractedFileOutputStream = new FileOutputStream(entry.getName());
                try {
                    IOUtils.copy(contentInputStream, extractedFileOutputStream);
                } finally {
                    IOUtils.closeQuietly(contentInputStream);
                    IOUtils.closeQuietly(extractedFileOutputStream);
                }
                FileTime fileTime = FileTime.fromMillis(entry.getLastModifiedDate().getTime());
                Files.setLastModifiedTime(file, fileTime);
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.alfresco.repo.action.executer.ImporterActionExecuter.java

/**
 * Extract the file and folder structure of a ZIP file into the specified directory
 * //from  www  . j av  a2 s  . co  m
 * @param archive       The ZIP archive to extract
 * @param extractDir    The directory to extract into
 */
public static void extractFile(ZipFile archive, String extractDir) {
    String fileName;
    String destFileName;
    byte[] buffer = new byte[BUFFER_SIZE];
    extractDir = extractDir + File.separator;
    try {
        for (Enumeration e = archive.getEntries(); e.hasMoreElements();) {
            ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
            if (!entry.isDirectory()) {
                fileName = entry.getName();
                fileName = fileName.replace('/', File.separatorChar);

                if (fileName.startsWith("/") || fileName.indexOf(":" + File.separator) == 1
                        || fileName.contains(".." + File.separator)) {
                    throw new AlfrescoRuntimeException(ARCHIVE_CONTAINS_SUSPICIOUS_PATHS_ERROR);
                }

                destFileName = extractDir + fileName;
                File destFile = new File(destFileName);
                String parent = destFile.getParent();
                if (parent != null) {
                    File parentFile = new File(parent);
                    if (!parentFile.exists())
                        parentFile.mkdirs();
                }
                InputStream in = new BufferedInputStream(archive.getInputStream(entry), BUFFER_SIZE);
                OutputStream out = new BufferedOutputStream(new FileOutputStream(destFileName), BUFFER_SIZE);
                int count;
                while ((count = in.read(buffer)) != -1) {
                    out.write(buffer, 0, count);
                }
                in.close();
                out.close();
            } else {
                File newdir = new File(extractDir + entry.getName());
                newdir.mkdirs();
            }
        }
    } catch (ZipException e) {
        throw new AlfrescoRuntimeException("Failed to process ZIP file.", e);
    } catch (FileNotFoundException e) {
        throw new AlfrescoRuntimeException("Failed to process ZIP file.", e);
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Failed to process ZIP file.", e);
    }
}

From source file:org.apache.ambari.view.slider.SliderAppsViewControllerImpl.java

private List<SliderAppType> loadAppTypes() {
    List<SliderAppType> appTypes = null;
    String appsFolderPath = getAppsFolderPath();
    File appsFolder = new File(appsFolderPath);
    if (appsFolder.exists()) {
        File[] appZips = appsFolder.listFiles((FilenameFilter) new RegexFileFilter("^.*\\.zip$"));
        if (appZips != null) {
            appTypes = new ArrayList<SliderAppType>();
            for (File appZip : appZips) {
                try {
                    ZipFile zipFile = new ZipFile(appZip);
                    Metainfo metainfo = new MetainfoParser()
                            .parse(zipFile.getInputStream(zipFile.getEntry("metainfo.xml")));
                    // Create app type object
                    if (metainfo.getApplication() != null) {
                        Application application = metainfo.getApplication();
                        ZipArchiveEntry appConfigZipEntry = zipFile.getEntry("appConfig-default.json");
                        ZipArchiveEntry appConfigSecuredZipEntry = zipFile
                                .getEntry("appConfig-secured-default.json");
                        if (appConfigZipEntry == null) {
                            throw new IllegalStateException("Slider App package '" + appZip.getName()
                                    + "' does not contain 'appConfig-default.json' file");
                        }//from w w w .jav  a  2 s.  c  o  m
                        ZipArchiveEntry resourcesZipEntry = zipFile.getEntry("resources-default.json");
                        ZipArchiveEntry resourcesSecuredZipEntry = zipFile
                                .getEntry("resources-secured-default.json");
                        if (resourcesZipEntry == null) {
                            throw new IllegalStateException("Slider App package '" + appZip.getName()
                                    + "' does not contain 'resources-default.json' file");
                        }
                        SliderAppType appType = new SliderAppType();
                        appType.setId(application.getName());
                        appType.setTypeName(application.getName());
                        appType.setTypeDescription(application.getComment());
                        appType.setTypeVersion(application.getVersion());
                        appType.setTypePackageFileName(appZip.getName());
                        // Configs
                        appType.typeConfigsUnsecured = parseAppTypeConfigs(zipFile, appConfigZipEntry,
                                appZip.getName(), application.getName());
                        if (appConfigSecuredZipEntry != null) {
                            appType.typeConfigsSecured = parseAppTypeConfigs(zipFile, appConfigSecuredZipEntry,
                                    appZip.getName(), application.getName());
                        }
                        // Resources
                        appType.resourcesUnsecured = parseAppTypeResources(zipFile, resourcesZipEntry);
                        if (resourcesSecuredZipEntry != null) {
                            appType.resourcesSecured = parseAppTypeResources(zipFile, resourcesSecuredZipEntry);
                        }
                        // Components
                        ArrayList<SliderAppTypeComponent> appTypeComponentList = new ArrayList<SliderAppTypeComponent>();
                        for (Component component : application.getComponents()) {
                            if ("CLIENT".equals(component.getCategory())) {
                                continue;
                            }
                            SliderAppTypeComponent appTypeComponent = new SliderAppTypeComponent();
                            appTypeComponent.setDisplayName(component.getName());
                            appTypeComponent.setId(component.getName());
                            appTypeComponent.setName(component.getName());
                            appTypeComponent.setYarnMemory(1024);
                            appTypeComponent.setYarnCpuCores(1);
                            // Updated below if present in resources.json
                            appTypeComponent.setInstanceCount(1);
                            // appTypeComponent.setPriority(component.);
                            if (component.getMinInstanceCount() != null) {
                                appTypeComponent
                                        .setInstanceCount(Integer.parseInt(component.getMinInstanceCount()));
                            }
                            if (component.getMaxInstanceCount() != null) {
                                appTypeComponent
                                        .setMaxInstanceCount(Integer.parseInt(component.getMaxInstanceCount()));
                            }
                            appTypeComponent.setCategory(component.getCategory());
                            appTypeComponentList.add(appTypeComponent);
                        }

                        MetricsHolder metricsHolder = new MetricsHolder();
                        metricsHolder.setJmxMetrics(readMetrics(zipFile, "jmx_metrics.json"));
                        metricsHolder.setTimelineMetrics(readMetrics(zipFile, "timeline_metrics.json"));
                        appType.setSupportedMetrics(getSupportedMetrics(metricsHolder.getTimelineMetrics()));
                        appMetrics.put(appType.uniqueName(), metricsHolder);

                        appType.setTypeComponents(appTypeComponentList);
                        appTypes.add(appType);
                    }
                } catch (ZipException e) {
                    logger.warn("Unable to parse Slider App package " + appZip.getAbsolutePath(), e);
                } catch (IOException e) {
                    logger.warn("Unable to parse Slider App package " + appZip.getAbsolutePath(), e);
                } catch (Throwable e) {
                    logger.warn("Unable to parse Slider App package " + appZip.getAbsolutePath(), e);
                }
            }
        }
    }
    return appTypes;
}

From source file:org.apache.ambari.view.slider.SliderAppsViewControllerImpl.java

private JsonObject parseAppTypeResources(ZipFile zipFile, ZipArchiveEntry resourcesZipEntry)
        throws ZipException, IOException {
    String resourcesJsonString = IOUtils.toString(zipFile.getInputStream(resourcesZipEntry), "UTF-8");
    JsonElement resourcesJson = new JsonParser().parse(resourcesJsonString);
    return resourcesJson.getAsJsonObject().get("components").getAsJsonObject();
}

From source file:org.apache.ambari.view.slider.SliderAppsViewControllerImpl.java

private Map<String, String> parseAppTypeConfigs(ZipFile zipFile, ZipArchiveEntry appConfigZipEntry,
        String zipFileName, String appName) throws IOException, ZipException {
    String appConfigJsonString = IOUtils.toString(zipFile.getInputStream(appConfigZipEntry), "UTF-8");
    JsonElement appConfigJson = new JsonParser().parse(appConfigJsonString);
    Map<String, String> configsMap = new HashMap<String, String>();
    JsonObject appTypeGlobalJson = appConfigJson.getAsJsonObject().get("global").getAsJsonObject();
    for (Entry<String, JsonElement> e : appTypeGlobalJson.entrySet()) {
        String key = e.getKey();/*from w ww.  j a v a 2  s  . c  o m*/
        String valueString = e.getValue().getAsString();
        if ("application.def".equals(key)) {
            valueString = String.format(".slider/package/%s/%s", appName, zipFileName);
        }
        configsMap.put(key, valueString);
    }
    return configsMap;
}

From source file:org.apache.ambari.view.slider.SliderAppsViewControllerImpl.java

Map<String, Map<String, Map<String, Metric>>> readMetrics(ZipFile zipFile, String fileName) {
    Map<String, Map<String, Map<String, Metric>>> metrics = null;
    try {/*  ww w  .  j a v  a 2s . c om*/
        InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(fileName));
        ObjectMapper mapper = new ObjectMapper();

        metrics = mapper.readValue(inputStream,
                new TypeReference<Map<String, Map<String, Map<String, Metric>>>>() {
                });
    } catch (IOException e) {
        logger.info("Error reading metrics for file " + fileName + ". " + e.getMessage());
    }

    return metrics;
}