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:org.jwebsocket.util.Tools.java

/**
 * Uncompress a byte array using zip compression
 *
 * @param aBA// w ww.  j a  v  a2s. co  m
 * @param aBase64Decode If TRUE, the byte array is Base64 decoded before uncompress
 * @return
 * @throws Exception
 */
public static byte[] unzip(byte[] aBA, Boolean aBase64Decode) throws Exception {
    if (aBase64Decode) {
        aBA = Base64.decodeBase64(aBA);
    }
    ByteArrayInputStream lBAIS = new ByteArrayInputStream(aBA);
    ZipArchiveInputStream lAIOS = new ZipArchiveInputStream(lBAIS);
    // ATTENTION: do not comment next line!!!
    lAIOS.getNextZipEntry();

    ByteArrayOutputStream lBAOS = new ByteArrayOutputStream();
    IOUtils.copy(lAIOS, lBAOS);
    lAIOS.close();

    return lBAOS.toByteArray();
}

From source file:org.metaservice.core.file.archives.ArchiveExtractionProcessor.java

@Override
public void process(@NotNull org.openrdf.model.URI uri, @NotNull RepositoryConnection resultConnection,
        Date time) throws PostProcessorException {
    String mimeType = retrievMimeType(uri);

    //todo check if mimetype is archive
    try (FileInputStream fileInputStream = new FileInputStream(FileUriUtils.getFile(uri).toFile())) {
        if (mimeType.equals("xz")) {
            XZInputStream xzInputStream = new XZInputStream(fileInputStream);
            processExtration(xzInputStream, uri, resultConnection);
        } else if (mimeType.equals("gz")) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
            processExtration(gzipInputStream, uri, resultConnection);
        } else if (mimeType.equals("bz2")) {
            BZip2CompressorInputStream bZip2CompressorInputStream = new BZip2CompressorInputStream(
                    fileInputStream);// ww  w .j  a v a 2 s. co  m
            processExtration(bZip2CompressorInputStream, uri, resultConnection);
        } else if (mimeType.equals("zip")) {
            //todo also process as archive
            ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fileInputStream);
            processArchiveExtraction(zipInputStream, uri, resultConnection);
        } else if (mimeType.equals("tar")) {
            TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(fileInputStream);
            processArchiveExtraction(tarArchiveInputStream, uri, resultConnection);
        }

    } catch (IOException | RepositoryException | FileProcessingException e) {
        throw new PostProcessorException(e);
    }

}

From source file:org.opencastproject.ingest.impl.IngestServiceImpl.java

/**
 * {@inheritDoc}//from   www  .j  a v  a  2  s  .c  o m
 * 
 * @see org.opencastproject.ingest.api.IngestService#addZippedMediaPackage(java.io.InputStream, java.lang.String,
 *      java.util.Map, java.lang.Long)
 */
@Override
public WorkflowInstance addZippedMediaPackage(InputStream zipStream, String workflowDefinitionId,
        Map<String, String> workflowConfig, Long workflowInstanceId)
        throws MediaPackageException, IOException, IngestException, NotFoundException, UnauthorizedException {
    // Start a job synchronously. We can't keep the open input stream waiting around.
    Job job = null;

    // Get hold of the workflow instance if specified
    WorkflowInstance workflowInstance = null;
    if (workflowInstanceId != null) {
        logger.info("Ingesting zipped mediapackage for workflow {}", workflowInstanceId);
        try {
            workflowInstance = workflowService.getWorkflowById(workflowInstanceId);
        } catch (NotFoundException e) {
            logger.debug("Ingest target workflow not found, starting a new one");
        } catch (WorkflowDatabaseException e) {
            throw new IngestException(e);
        }
    } else {
        logger.info("Ingesting zipped mediapackage");
    }

    ZipArchiveInputStream zis = null;
    Set<String> collectionFilenames = new HashSet<String>();
    try {
        // We don't need anybody to do the dispatching for us. Therefore we need to make sure that the job is never in
        // QUEUED state but set it to INSTANTIATED in the beginning and then manually switch it to RUNNING.
        job = serviceRegistry.createJob(JOB_TYPE, INGEST_STREAM, null, null, false);
        job.setStatus(Status.RUNNING);
        serviceRegistry.updateJob(job);

        // Create the working file target collection for this ingest operation
        String wfrCollectionId = Long.toString(job.getId());

        zis = new ZipArchiveInputStream(zipStream);
        ZipArchiveEntry entry;
        MediaPackage mp = null;
        Map<String, URI> uris = new HashMap<String, URI>();
        // Sequential number to append to file names so that, if two files have the same
        // name, one does not overwrite the other
        int seq = 1;
        // While there are entries write them to a collection
        while ((entry = zis.getNextZipEntry()) != null) {
            try {
                if (entry.isDirectory() || entry.getName().contains("__MACOSX"))
                    continue;

                if (entry.getName().endsWith("manifest.xml") || entry.getName().endsWith("index.xml")) {
                    // Build the mediapackage
                    mp = loadMediaPackageFromManifest(new ZipEntryInputStream(zis, entry.getSize()));
                } else {
                    logger.info("Storing zip entry {} in working file repository collection '{}'",
                            job.getId() + entry.getName(), wfrCollectionId);
                    // Since the directory structure is not being mirrored, makes sure the file
                    // name is different than the previous one(s) by adding a sequential number
                    String fileName = FilenameUtils.getBaseName(entry.getName()) + "_" + seq++ + "."
                            + FilenameUtils.getExtension(entry.getName());
                    URI contentUri = workingFileRepository.putInCollection(wfrCollectionId, fileName,
                            new ZipEntryInputStream(zis, entry.getSize()));
                    collectionFilenames.add(fileName);
                    // Each entry name starts with zip file name; discard it so that the map key will match the media package
                    // element uri
                    String key = entry.getName().substring(entry.getName().indexOf('/') + 1);
                    uris.put(key, contentUri);
                    ingestStatistics.add(entry.getSize());
                    logger.info("Zip entry {} stored at {}", job.getId() + entry.getName(), contentUri);
                }
            } catch (IOException e) {
                logger.warn("Unable to process zip entry {}: {}", entry.getName(), e.getMessage());
                throw e;
            }
        }

        if (mp == null)
            throw new MediaPackageException("No manifest found in this zip");

        // Determine the mediapackage identifier
        String mediaPackageId = null;
        if (workflowInstance != null) {
            mediaPackageId = workflowInstance.getMediaPackage().getIdentifier().toString();
            mp.setIdentifier(workflowInstance.getMediaPackage().getIdentifier());
        } else {
            if (mp.getIdentifier() == null || StringUtils.isBlank(mp.getIdentifier().toString()))
                mp.setIdentifier(new UUIDIdBuilderImpl().createNew());
            mediaPackageId = mp.getIdentifier().toString();
        }

        logger.info("Ingesting mediapackage {} is named '{}'", mediaPackageId, mp.getTitle());

        // Make sure there are tracks in the mediapackage
        if (mp.getTracks().length == 0) {
            logger.warn("Mediapackage {} has no media tracks", mediaPackageId);
        }

        // Update the element uris to point to their working file repository location
        for (MediaPackageElement element : mp.elements()) {
            URI uri = uris.get(element.getURI().toString());

            if (uri == null)
                throw new MediaPackageException(
                        "Unable to map element name '" + element.getURI() + "' to workspace uri");
            logger.info("Ingested mediapackage element {}/{} is located at {}",
                    new Object[] { mediaPackageId, element.getIdentifier(), uri });
            URI dest = workingFileRepository.moveTo(wfrCollectionId, uri.toString(), mediaPackageId,
                    element.getIdentifier(), FilenameUtils.getName(element.getURI().toString()));
            element.setURI(dest);

            // TODO: This should be triggered somehow instead of being handled here
            if (MediaPackageElements.SERIES.equals(element.getFlavor())) {
                logger.info("Ingested mediapackage {} contains updated series information", mediaPackageId);
                updateSeries(element.getURI());
            }
        }

        // Now that all elements are in place, start with ingest
        logger.info("Initiating processing of ingested mediapackage {}", mediaPackageId);
        workflowInstance = ingest(mp, workflowDefinitionId, workflowConfig, workflowInstanceId);
        logger.info("Ingest of mediapackage {} done", mediaPackageId);
        job.setStatus(Job.Status.FINISHED);
        return workflowInstance;

    } catch (ServiceRegistryException e) {
        throw new IngestException(e);
    } catch (MediaPackageException e) {
        job.setStatus(Job.Status.FAILED, Job.FailureReason.DATA);
        if (workflowInstance != null) {
            workflowInstance.getCurrentOperation().setState(OperationState.FAILED);
            workflowInstance.setState(WorkflowState.FAILED);
            try {
                logger.info("Marking related workflow {} as failed", workflowInstance);
                workflowService.update(workflowInstance);
            } catch (WorkflowException e1) {
                logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance,
                        e1.getMessage());
            }
        }
        throw e;
    } catch (Exception e) {
        job.setStatus(Job.Status.FAILED);
        if (workflowInstance != null) {
            workflowInstance.getCurrentOperation().setState(OperationState.FAILED);
            workflowInstance.setState(WorkflowState.FAILED);
            try {
                logger.info("Marking related workflow {} as failed", workflowInstance);
                workflowService.update(workflowInstance);
            } catch (WorkflowException e1) {
                logger.error("Error updating workflow instance {} with ingest failure: {}", workflowInstance,
                        e1.getMessage());
            }
        }
        if (e instanceof IngestException)
            throw (IngestException) e;
        throw new IngestException(e);
    } finally {
        IOUtils.closeQuietly(zis);
        for (String filename : collectionFilenames) {
            workingFileRepository.deleteFromCollection(Long.toString(job.getId()), filename);
        }
        try {
            serviceRegistry.updateJob(job);
        } catch (Exception e) {
            throw new IngestException("Unable to update job", e);
        }
    }
}

From source file:org.openengsb.labs.paxexam.karaf.container.internal.KarafTestContainer.java

private void extractZipDistribution(URL sourceDistribution, File targetFolder) throws IOException {
    extract(new ZipArchiveInputStream(sourceDistribution.openStream()), targetFolder);
}

From source file:org.openengsb.openengsbplugin.Provision.java

private void extractArchive() throws MojoFailureException {
    try {//ww  w .j a  v a  2s . c o  m
        extract(new ZipArchiveInputStream(new FileInputStream(provisionArchivePathWindows)), new File(RUNNER));
    } catch (FileNotFoundException e) {
        throw new MojoFailureException(e,
                "Provision file for WINDOWS could not be found (" + provisionArchivePathWindows + ")",
                e.getMessage());
    } catch (IOException e) {
        throw new MojoFailureException(e,
                "Provision file for WINDOWS could not be found (" + provisionArchivePathWindows + ")",
                e.getMessage());
    }
}

From source file:org.ops4j.pax.exam.karaf.container.internal.ArchiveExtractor.java

private static void extractZipDistribution(URL sourceDistribution, File _targetFolder) throws IOException {
    extract(new ZipArchiveInputStream(sourceDistribution.openStream()), _targetFolder);
}

From source file:org.owasp.dependencycheck.analyzer.ArchiveAnalyzer.java

/**
 * Extracts the contents of an archive into the specified directory.
 *
 * @param archive an archive file such as a WAR or EAR
 * @param destination a directory to extract the contents to
 * @param engine the scanning engine//ww  w .  ja  v  a2s. c  om
 * @throws AnalysisException thrown if the archive is not found
 */
private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException {
    if (archive == null || destination == null) {
        return;
    }

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(archive);
    } catch (FileNotFoundException ex) {
        LOGGER.log(Level.FINE, null, ex);
        throw new AnalysisException("Archive file was not found.", ex);
    }
    final String archiveExt = FileUtils.getFileExtension(archive.getName()).toLowerCase();
    try {
        if (ZIPPABLES.contains(archiveExt)) {
            extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
        } else if ("tar".equals(archiveExt)) {
            extractArchive(new TarArchiveInputStream(new BufferedInputStream(fis)), destination, engine);
        } else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) {
            final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName());
            final String uncompressedExt = FileUtils.getFileExtension(uncompressedName).toLowerCase();
            if (engine.supportsExtension(uncompressedExt)) {
                decompressFile(new GzipCompressorInputStream(new BufferedInputStream(fis)),
                        new File(destination, uncompressedName));
            }
        }
    } catch (ArchiveExtractionException ex) {
        final String msg = String.format("Exception extracting archive '%s'.", archive.getName());
        LOGGER.log(Level.WARNING, msg);
        LOGGER.log(Level.FINE, null, ex);
    } catch (IOException ex) {
        final String msg = String.format("Exception reading archive '%s'.", archive.getName());
        LOGGER.log(Level.WARNING, msg);
        LOGGER.log(Level.FINE, null, ex);
    } finally {
        try {
            fis.close();
        } catch (IOException ex) {
            LOGGER.log(Level.FINEST, null, ex);
        }
    }
}

From source file:org.owasp.dependencycheck.utils.ExtractionUtil.java

/**
 * Extracts the contents of an archive into the specified directory.
 *
 * @param archive an archive file such as a WAR or EAR
 * @param destination a directory to extract the contents to
 * @param filter determines which files get extracted
 * @throws ExtractionException thrown if the archive is not found
 *///from   www . j  av a  2s  . c  o  m
public static void extractFilesUsingFilter(File archive, File destination, FilenameFilter filter)
        throws ExtractionException {
    if (archive == null || destination == null) {
        return;
    }

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(archive);
    } catch (FileNotFoundException ex) {
        LOGGER.debug("", ex);
        throw new ExtractionException("Archive file was not found.", ex);
    }
    try {
        extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, filter);
    } catch (ArchiveExtractionException ex) {
        LOGGER.warn("Exception extracting archive '{}'.", archive.getName());
        LOGGER.debug("", ex);
    } finally {
        try {
            fis.close();
        } catch (IOException ex) {
            LOGGER.debug("", ex);
        }
    }
}

From source file:org.panbox.core.identitymgmt.VCardProtector.java

/**
 * Method extracts the VCF file stored within the zipped import file to the
 * given destination file. It further returns the corresponding hmac stored
 * within the archive./*from w  w w.jav  a2  s.  c o m*/
 * 
 * @param sourceFile
 *            import archive
 * @param tmpFile
 *            temporary file to extract the CVF to
 * @return byte[] array containing the hmac of the VCF
 * @throws Exception
 */
public static byte[] unwrapVCF(File sourceFile, File tmpFile) throws FileNotFoundException, IOException {

    ZipArchiveInputStream in = null;
    FileOutputStream fos = null;
    String hmacString = null;
    try {

        in = new ZipArchiveInputStream(new FileInputStream(sourceFile));
        ArchiveEntry entry;
        // ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // ENTRY 1: vcard contents
        in.getNextEntry();
        fos = new FileOutputStream(tmpFile);
        IOUtils.copy(in, fos);

        // ENTRY 2: sha-256 hmac
        entry = in.getNextEntry();
        hmacString = entry.getName();

        return Utils.hexToBytes(hmacString);
    } catch (StringIndexOutOfBoundsException e) {
        logger.error("Error parsing hmac: " + hmacString + " is no valid hex String", e);
        throw e;
    } catch (Exception e) {
        logger.error("Error unwrapping VCF file", e);
        throw e;
    } finally {
        if (fos != null) {
            fos.flush();
            fos.close();
        }
        if (in != null) {
            in.close();
        }
    }
}

From source file:org.panbox.core.pairing.file.PanboxFilePairingUtils.java

public static PanboxFilePairingLoadReturnContainer loadPairingFile(File inputFile, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException,
        UnrecoverableKeyException, IllegalArgumentException {
    ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(inputFile));
    try {/*from  w  ww.  j  a  v a 2s  .c o  m*/
        byte[] buffer = new byte[1048576]; //1MB

        ArchiveEntry entry;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;

        // ENTRY 1: devicename
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for device name.");
            throw new IllegalArgumentException("Could not find entry for device name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String devicename = new String(baos.toByteArray());

        // ENTRY 2: eMail
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for eMail.");
            throw new IllegalArgumentException("Could not find entry for eMail.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String eMail = new String(baos.toByteArray());

        // ENTRY 3: firstName
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for first name.");
            throw new IllegalArgumentException("Could not find entry for first name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String firstName = new String(baos.toByteArray());

        // ENTRY 4: lastName
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for last name.");
            throw new IllegalArgumentException("Could not find entry for last name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String lastName = new String(baos.toByteArray());

        // ENTRY 5: devKeyStore.p12
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for device key store.");
            throw new IllegalArgumentException("Could not find entry for device key store.");
        }

        KeyStore devKeyStore = KeyStore.getInstance("PKCS12");
        devKeyStore.load(in, password);
        PrivateKey devPKey = (PrivateKey) devKeyStore.getKey(devicename.toLowerCase(), password);
        Certificate[] devCert = devKeyStore.getCertificateChain(devicename.toLowerCase());

        // ENTRY 6: knownDevices.list/knownDevices.bks
        entry = in.getNextEntry(); // knownDevices.list

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for knownDevices.list.");
            throw new IllegalArgumentException("Could not find entry for knownDevices.list.");
        }

        Map<String, X509Certificate> devices = new HashMap<String, X509Certificate>();

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        Map<String, String> deviceNames = new HashMap<String, String>();

        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(DELIMITER);
            deviceNames.put(values[0], values[1]);
        }

        entry = in.getNextEntry(); // knownDevices.bks

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for knownDevices.bks.");
            throw new IllegalArgumentException("Could not find entry for knownDevices.bks.");
        }

        KeyStore devicesStore = KeyStore.getInstance("BKS");
        devicesStore.load(in, password);

        for (Entry<String, String> device : deviceNames.entrySet()) {
            X509Certificate deviceCert = (X509Certificate) devicesStore.getCertificate(device.getKey());
            devices.put(device.getValue(), deviceCert);
        }

        // ENTRY 7: contacts.vcard
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for contacts.");
            throw new IllegalArgumentException("Could not find entry for contacts.");
        }

        File contacts = File.createTempFile("panbox" + (new Random().nextInt(65536) - 32768), null);
        FileOutputStream fos = new FileOutputStream(contacts);
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();

        // ENTRY 8: ownerKeyStore/ownerCertStore.jks
        entry = in.getNextEntry();

        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        IOUtils.copy(in, tmp);
        ByteArrayInputStream buf = new ByteArrayInputStream(tmp.toByteArray());

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for owner key store.");
            throw new IllegalArgumentException("Could not find entry for owner key store.");
        }

        KeyStore ownerKeyStore = null;
        try {
            // Check if pairing is MASTER
            ownerKeyStore = KeyStore.getInstance("PKCS12");
            ownerKeyStore.load(buf, password);
            // At this point we know it's a PKCS11 file!
            PrivateKey ownerEncKey = (PrivateKey) ownerKeyStore.getKey("ownerEncKey", password);
            Certificate[] ownerEncCert = ownerKeyStore.getCertificateChain("ownerEncKey");
            PrivateKey ownerSignKey = (PrivateKey) ownerKeyStore.getKey("ownerSignKey", password);
            Certificate[] ownerSignCert = ownerKeyStore.getCertificateChain("ownerSignKey");
            in.close();
            removeInputFile(inputFile);

            return new PanboxFilePairingLoadReturnContainer(eMail, firstName, lastName, password, devicename,
                    devPKey, devCert[0], ownerSignKey, ownerSignCert[0], ownerEncKey, ownerEncCert[0], devices,
                    contacts);
        } catch (Exception e) {
            // SLAVE
            try {
                buf = new ByteArrayInputStream(tmp.toByteArray());
                ownerKeyStore = KeyStore.getInstance("BKS");
                ownerKeyStore.load(buf, password);
                Certificate ownerEncCert = ownerKeyStore.getCertificate("ownerEncCert");
                Certificate ownerSignCert = ownerKeyStore.getCertificate("ownerSignCert");
                in.close();
                removeInputFile(inputFile);

                return new PanboxFilePairingLoadReturnContainer(eMail, firstName, lastName, password,
                        devicename, devPKey, devCert[0], null, ownerSignCert, null, ownerEncCert, devices,
                        contacts);
            } catch (Exception ex) {
                logger.error(
                        "PanboxClient : loadPairingFile : Could not determine if pairing file was master or slave.");
                throw new IllegalArgumentException("Pairing type was unknown. Broken file?");
            }
        }
    } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException
            | UnrecoverableKeyException | IllegalArgumentException e) {
        in.close();
        throw e;
    }

}