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

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

Introduction

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

Prototype

public long getSize() 

Source Link

Document

Returns the uncompressed size of the entry data.

Usage

From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java

public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException {
    final FileObject file = this;
    final XMLEventFactory ef = XMLEventFactory.newInstance();
    final byte[] buf = new byte[1024];
    final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream());
    return new XMLEventReaderBase() {
        private boolean started;
        private boolean ended;

        public void close() throws XMLStreamException {
            try {
                zip.close();//from  ww w . j  a  va 2 s  .c  om
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }

        protected boolean more() throws XMLStreamException {
            try {
                ZipArchiveEntry entry;
                if (!started) {
                    Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI());
                    add(ef.createStartDocument());
                    add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator()));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(file.getName()));
                    add(ef.createEndElement(TITLE, null));
                    add(ef.createStartElement(ID, null, null));
                    add(ef.createCharacters(id));
                    add(ef.createEndElement(ID, null));
                    Attribute href = ef.createAttribute("href", file.toUri().toASCIIString());
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip"));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    add(ef.createStartElement(UPDATED, null, null));
                    add(ef.createCharacters(format(new Date(file.getLastModified()))));
                    add(ef.createEndElement(UPDATED, null));
                    started = true;
                    return true;
                } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) {
                    String name = entry.getName();
                    String link = entryPattern.replace("{entry}", PercentCodec.encode(name));
                    MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap();
                    String type = mimetypes.getContentType(name);
                    if (type == null || type.length() == 0) {
                        type = "application/octet-stream";
                    }
                    add(ef.createStartElement(ENTRY, null, null));
                    add(ef.createStartElement(TITLE, null, null));
                    add(ef.createCharacters(name));
                    add(ef.createEndElement(TITLE, null));
                    Attribute href = ef.createAttribute("href", link);
                    List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type));
                    add(ef.createStartElement(LINK, attrs.iterator(), null));
                    add(ef.createEndElement(LINK, null));
                    long size = entry.getSize();
                    if (size > 0) {
                        zip.skip(size);
                    } else {
                        while (zip.read(buf, 0, buf.length) >= 0)
                            ;
                    }
                    add(ef.createEndElement(ENTRY, null));
                    return true;
                } else if (!ended) {
                    add(ef.createEndElement(FEED, null));
                    add(ef.createEndDocument());
                    ended = true;
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }
        }
    };
}

From source file:org.cloudfoundry.util.ResourceMatchingUtils.java

private static Flux<ArtifactMetadata> getArtifactMetadataFromZip(Path application) {
    List<ArtifactMetadata> artifactMetadatas = new ArrayList<>();

    try (ZipFile zipFile = new ZipFile(application.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();

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

            if (!entry.isDirectory()) {
                try (InputStream in = zipFile.getInputStream(entry)) {
                    String hash = FileUtils.hash(in);
                    String path = entry.getName();
                    String permissions = FileUtils.permissions(entry.getUnixMode());
                    int size = (int) entry.getSize();

                    artifactMetadatas.add(new ArtifactMetadata(hash, path, permissions, size));
                }//  w ww  .  j av  a  2s .  c  om

            }
        }
    } catch (IOException e) {
        throw Exceptions.propagate(e);
    }

    return Flux.fromIterable(artifactMetadatas);
}

From source file:org.codelibs.fess.crawler.extractor.impl.ZipExtractor.java

@Override
public ExtractData getText(final InputStream in, final Map<String, String> params) {
    if (in == null) {
        throw new CrawlerSystemException("The inputstream is null.");
    }//from  ww  w  .java  2  s  .  c  o m

    final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper");
    if (mimeTypeHelper == null) {
        throw new CrawlerSystemException("MimeTypeHelper is unavailable.");
    }

    final ExtractorFactory extractorFactory = crawlerContainer.getComponent("extractorFactory");
    if (extractorFactory == null) {
        throw new CrawlerSystemException("ExtractorFactory is unavailable.");
    }

    final UnsafeStringBuilder buf = new UnsafeStringBuilder(1000);

    ArchiveInputStream ais = null;

    try {
        ais = archiveStreamFactory.createArchiveInputStream(in);
        ZipArchiveEntry entry = null;
        long contentSize = 0;
        while ((entry = (ZipArchiveEntry) ais.getNextEntry()) != null) {
            contentSize += entry.getSize();
            if (maxContentSize != -1 && contentSize > maxContentSize) {
                throw new MaxLengthExceededException(
                        "Extracted size is " + contentSize + " > " + maxContentSize);
            }
            final String filename = entry.getName();
            final String mimeType = mimeTypeHelper.getContentType(null, filename);
            if (mimeType != null) {
                final Extractor extractor = extractorFactory.getExtractor(mimeType);
                if (extractor != null) {
                    try {
                        final Map<String, String> map = new HashMap<String, String>();
                        map.put(TikaMetadataKeys.RESOURCE_NAME_KEY, filename);
                        buf.append(extractor.getText(new IgnoreCloseInputStream(ais), map).getContent());
                        buf.append('\n');
                    } catch (final Exception e) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Exception in an internal extractor.", e);
                        }
                    }
                }
            }
        }
    } catch (final MaxLengthExceededException e) {
        throw e;
    } catch (final Exception e) {
        if (buf.length() == 0) {
            throw new ExtractException("Could not extract a content.", e);
        }
    } finally {
        IOUtils.closeQuietly(ais);
    }

    return new ExtractData(buf.toUnsafeString().trim());
}

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

/**
 * {@inheritDoc}/*from   www  . jav  a 2s .  com*/
 * 
 * @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.sead.nds.repository.util.ValidationJob.java

private String generateFileHash(String name, ZipFile zf) {

    ZipArchiveEntry archiveEntry1 = zf.getEntry(name);
    // Error check - add file sizes to compare against supplied stats

    long start = System.currentTimeMillis();
    InputStream inputStream = null;
    String realHash = null;//from w  w w  .j a v a 2 s .c  o  m
    try {
        inputStream = zf.getInputStream(archiveEntry1);
        String hashtype = bagGenerator.getHashtype();
        if (hashtype != null) {
            if (hashtype.equals("SHA1 Hash")) {
                realHash = DigestUtils.sha1Hex(inputStream);
            } else if (hashtype.equals("SHA512 Hash")) {
                realHash = DigestUtils.sha512Hex(inputStream);
            }
        }

    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    log.debug("Retrieve/compute time = " + (System.currentTimeMillis() - start) + " ms");
    // Error check - add file sizes to compare against supplied stats
    bagGenerator.incrementTotalDataSize(archiveEntry1.getSize());
    return realHash;
}

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;//from  www. j a  va 2s  .  c om
    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:stroom.util.zip.StroomZipFile.java

public StroomZipNameSet getStroomZipNameSet() throws IOException {
    if (stroomZipNameSet == null) {
        stroomZipNameSet = new StroomZipNameSet(false);
        Enumeration<ZipArchiveEntry> entryE = getZipFile().getEntries();

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

            // Skip Dir's
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                stroomZipNameSet.add(fileName);
            }/*w w  w  .  ja v  a2  s  .c o m*/

            long entrySize = entry.getSize();
            if (entrySize == -1 || totalSize == -1) {
                // Can nolonger sum
            } else {
                totalSize += entrySize;
            }

        }

    }
    return stroomZipNameSet;
}