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

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

Introduction

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

Prototype

public ZipArchiveInputStream(InputStream inputStream) 

Source Link

Usage

From source file:fr.ens.biologie.genomique.eoulsan.modules.mapping.hadoop.ReadsMapperHadoopModule.java

/**
 * Compute the checksum of a ZIP file.//from   w w w .  j  a  v  a2 s  .  c  o  m
 * @param in input stream
 * @return the checksum as a string
 * @throws IOException if an error occurs while creating the checksum
 */
private static String computeZipCheckSum(final InputStream in) throws IOException {

    ZipArchiveInputStream zais = new ZipArchiveInputStream(in);

    // Create Hash function
    final Hasher hs = Hashing.md5().newHasher();

    // Store entries in a map
    final Map<String, long[]> map = new HashMap<>();

    ZipArchiveEntry e;

    while ((e = zais.getNextZipEntry()) != null) {
        map.put(e.getName(), new long[] { e.getSize(), e.getCrc() });
    }

    zais.close();

    // Add values to hash function in an ordered manner
    for (String filename : new TreeSet<>(map.keySet())) {

        hs.putString(filename, StandardCharsets.UTF_8);
        for (long l : map.get(filename)) {
            hs.putLong(l);
        }
    }

    return hs.hash().toString();
}

From source file:io.sloeber.core.managers.InternalPackageManager.java

private static IStatus processArchive(String pArchiveFileName, IPath pInstallPath, boolean pForceDownload,
        String pArchiveFullFileName, IProgressMonitor pMonitor) {
    // Create an ArchiveInputStream with the correct archiving algorithm
    String faileToExtractMessage = Messages.Manager_Failed_to_extract.replace(FILE, pArchiveFullFileName);
    if (pArchiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
        try (ArchiveInputStream inStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new FileInputStream(pArchiveFullFileName)))) {
            return extract(inStream, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }//  www  .  java  2 s.c  o m
    } else if (pArchiveFileName.endsWith("zip")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(pArchiveFullFileName))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else if (pArchiveFileName.endsWith("tar.gz")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new TarArchiveInputStream(
                new GzipCompressorInputStream(new FileInputStream(pArchiveFullFileName)))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else if (pArchiveFileName.endsWith("tar")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new TarArchiveInputStream(new FileInputStream(pArchiveFullFileName))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else {
        return new Status(IStatus.ERROR, Activator.getId(), Messages.Manager_Format_not_supported);
    }
}

From source file:io.sloeber.core.managers.Manager.java

private static IStatus processArchive(String pArchiveFileName, Path pInstallPath, boolean pForceDownload,
        String pArchiveFullFileName, IProgressMonitor pMonitor) {
    // Create an ArchiveInputStream with the correct archiving algorithm
    String faileToExtractMessage = Messages.Manager_Failed_to_extract + pArchiveFullFileName;
    if (pArchiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
        try (ArchiveInputStream inStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new FileInputStream(pArchiveFullFileName)))) {
            return extract(inStream, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }//from  w ww  .ja  va 2 s. c o m
    } else if (pArchiveFileName.endsWith("zip")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(pArchiveFullFileName))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else if (pArchiveFileName.endsWith("tar.gz")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new TarArchiveInputStream(
                new GzipCompressorInputStream(new FileInputStream(pArchiveFullFileName)))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else if (pArchiveFileName.endsWith("tar")) { //$NON-NLS-1$
        try (ArchiveInputStream in = new TarArchiveInputStream(new FileInputStream(pArchiveFullFileName))) {
            return extract(in, pInstallPath.toFile(), 1, pForceDownload, pMonitor);
        } catch (IOException | InterruptedException e) {
            return new Status(IStatus.ERROR, Activator.getId(), faileToExtractMessage, e);
        }
    } else {
        return new Status(IStatus.ERROR, Activator.getId(), Messages.Manager_Format_not_supported);
    }
}

From source file:it.evilsocket.dsploit.core.UpdateService.java

/**
 * open an Archive InputStream/*w  w  w .  ja  va 2  s.co m*/
 * @param in the InputStream to the archive
 * @return the ArchiveInputStream to read from
 * @throws IOException if an I/O error occurs
 * @throws java.lang.IllegalStateException if no archive method has been choose
 */
private ArchiveInputStream openArchiveStream(InputStream in) throws IOException, IllegalStateException {
    switch (mCurrentTask.archiver) {
    case tar:
        return new TarArchiveInputStream(new BufferedInputStream(openCompressedStream(in)));
    case zip:
        return new ZipArchiveInputStream(new BufferedInputStream(openCompressedStream(in)));
    default:
        throw new IllegalStateException("trying to open an archive, but no archive algorithm selected.");
    }
}

From source file:nz.co.kakariki.networkutils.reader.ExtractArchive.java

/**
 * Top unzip method.//from   w ww .  j  ava  2 s.  c o  m
 */
protected static void unzip(File zipfile) throws FileNotFoundException {
    File path = zipfile.getParentFile();
    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            String fn = z1.getName();
            if (fn.contains("/")) {
                fn = fn.substring(z1.getName().lastIndexOf("/"));
            }
            File f = new File(path + File.separator + fn);
            FileOutputStream fos = new FileOutputStream(f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int n = 0;
            byte[] content = new byte[BUFFER];
            while (-1 != (n = zais.read(content))) {
                fos.write(content, 0, n);
            }

            bos.flush();
            bos.close();
            fos.close();
        }

        zais.close();
        zipfile.delete();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:org.alfresco.repo.content.transform.AppleIWorksContentTransformer.java

@Override
protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options)
        throws Exception {
    final String sourceMimetype = reader.getMimetype();
    final String sourceExtension = getMimetypeService().getExtension(sourceMimetype);
    final String targetMimetype = writer.getMimetype();

    if (log.isDebugEnabled()) {
        StringBuilder msg = new StringBuilder();
        msg.append("Transforming from ").append(sourceMimetype).append(" to ").append(targetMimetype);
        log.debug(msg.toString());/*from   ww  w.j  a va 2s.  c om*/
    }

    ZipArchiveInputStream iWorksZip = null;
    try {
        // iWorks files are zip files (at least in recent versions, iWork 09).
        // If it's not a zip file, the resultant ZipException will be caught as an IOException below.
        iWorksZip = new ZipArchiveInputStream(reader.getContentInputStream());

        ZipArchiveEntry entry = null;
        boolean found = false;
        while (!found && (entry = iWorksZip.getNextZipEntry()) != null) {
            if (MimetypeMap.MIMETYPE_IMAGE_JPEG.equals(targetMimetype)
                    && entry.getName().equals(QUICK_LOOK_THUMBNAIL_JPG)) {
                writer.putContent(iWorksZip);
                found = true;
            } else if (MimetypeMap.MIMETYPE_PDF.equals(targetMimetype)
                    && entry.getName().equals(QUICK_LOOK_PREVIEW_PDF)) {
                writer.putContent(iWorksZip);
                found = true;
            }
        }

        if (!found) {
            throw new AlfrescoRuntimeException(
                    "Unable to transform " + sourceExtension + " file to " + targetMimetype);
        }
    } catch (FileNotFoundException e1) {
        throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e1);
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e);
    } finally {
        if (iWorksZip != null) {
            iWorksZip.close();
        }
    }
}

From source file:org.alfresco.repo.dictionary.CMMDownloadTestUtil.java

public Set<String> getDownloadEntries(final NodeRef downloadNode) {
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<Set<String>>() {
        @Override/*  w w  w .  j a  v a  2  s  . c o  m*/
        public Set<String> execute() throws Throwable {
            Set<String> entryNames = new TreeSet<String>();
            ContentReader reader = contentService.getReader(downloadNode, ContentModel.PROP_CONTENT);
            try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(
                    reader.getContentInputStream())) {
                ZipArchiveEntry zipEntry = null;
                while ((zipEntry = zipInputStream.getNextZipEntry()) != null) {
                    String name = zipEntry.getName();
                    entryNames.add(name);
                }
            }
            return entryNames;
        }
    });
}

From source file:org.alfresco.repo.download.DownloadServiceIntegrationTest.java

private Set<String> getEntries(final NodeRef downloadNode) {
    return TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Set<String>>() {

        @Override/*from ww w  .  j av  a  2  s. c  o  m*/
        public Set<String> execute() throws Throwable {
            Set<String> entryNames = new TreeSet<String>();
            ContentReader reader = CONTENT_SERVICE.getReader(downloadNode, ContentModel.PROP_CONTENT);
            ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(reader.getContentInputStream());
            try {
                ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry();
                while (zipEntry != null) {
                    String name = zipEntry.getName();
                    entryNames.add(name);
                    zipEntry = zipInputStream.getNextZipEntry();
                }
            } finally {
                zipInputStream.close();
            }
            return entryNames;
        }
    });
}

From source file:org.apache.karaf.decanter.kibana6.KibanaController.java

public void download() throws Exception {
    File target = new File(workingDirectory, KIBANA_FOLDER);
    if (target.exists()) {
        LOGGER.warn("Kibana folder already exists, download is skipped");
        return;/*w w w.j a v a  2 s .com*/
    }
    LOGGER.debug("Downloading Kibana from {}", KIBANA_LOCATION);
    if (isWindows()) {
        try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            ZipArchiveEntry entry;
            while ((entry = (ZipArchiveEntry) inputStream.getNextEntry()) != null) {
                File file = new File(workingDirectory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    int read;
                    byte[] buffer = new byte[4096];
                    try (FileOutputStream outputStream = new FileOutputStream(file)) {
                        while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                            outputStream.write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    } else {
        try (GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(
                new URL(KIBANA_LOCATION).openStream())) {
            try (TarArchiveInputStream inputStream = new TarArchiveInputStream(gzInputStream)) {
                TarArchiveEntry entry;
                while ((entry = (TarArchiveEntry) inputStream.getNextEntry()) != null) {
                    File file = new File(workingDirectory, entry.getName());
                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        int read;
                        byte[] buffer = new byte[4096];
                        try (FileOutputStream outputStream = new FileOutputStream(file)) {
                            while ((read = inputStream.read(buffer, 0, 4096)) != -1) {
                                outputStream.write(buffer, 0, read);
                            }
                        }
                        file.setLastModified(entry.getLastModifiedDate().getTime());
                        if (entry instanceof TarArchiveEntry) {
                            int mode = ((TarArchiveEntry) entry).getMode();
                            if ((mode & 00100) > 0) {
                                file.setExecutable(true, (mode & 00001) == 0);
                            }
                        }
                    }
                }
            }
        }
    }
    overrideConfig();
}

From source file:org.apache.karaf.kittests.Helper.java

protected static void extractWindowsKit(File targetDir) throws Exception {
    InputStream is = Helper.class.getResourceAsStream("/karaf.zip");
    extract(new ZipArchiveInputStream(is), targetDir);
}