Example usage for java.util.zip ZipEntry setSize

List of usage examples for java.util.zip ZipEntry setSize

Introduction

In this page you can find the example usage for java.util.zip ZipEntry setSize.

Prototype

public void setSize(long size) 

Source Link

Document

Sets the uncompressed size of the entry data.

Usage

From source file:org.docx4j.openpackaging.io3.stores.ZipPartStore.java

public void saveBinaryPart(Part part) throws Docx4JException {

    // Drop the leading '/'
    String resolvedPartUri = part.getPartName().getName().substring(1);

    try {//from  w  w  w .j a v  a  2  s.  co  m

        byte[] bytes = null;

        if (((BinaryPart) part).isLoaded()) {

            bytes = ((BinaryPart) part).getBytes();

        } else {

            if (this.sourcePartStore == null) {

                throw new Docx4JException("part store has changed, and sourcePartStore not set");

            } else if (this.sourcePartStore == this) {

                // Just use the ByteArray
                log.debug(part.getPartName() + " is clean");
                ByteArray byteArray = partByteArrays.get(part.getPartName().getName().substring(1));
                if (byteArray == null)
                    throw new IOException("part '" + part.getPartName() + "' not found");
                bytes = byteArray.getBytes();

            } else {

                InputStream is = sourcePartStore.loadPart(part.getPartName().getName().substring(1));
                bytes = IOUtils.toByteArray(is);
            }
        }

        // Add ZIP entry to output stream.
        if (part instanceof OleObjectBinaryPart) {
            // Workaround: Powerpoint 2010 (32-bit) can't play eg WMV if it is compressed!
            // (though 64-bit version is fine)

            ZipEntry ze = new ZipEntry(resolvedPartUri);
            ze.setMethod(ZipOutputStream.STORED);

            // must set size, compressed size, and crc-32
            ze.setSize(bytes.length);
            ze.setCompressedSize(bytes.length);

            CRC32 crc = new CRC32();
            crc.update(bytes);
            ze.setCrc(crc.getValue());

            zos.putNextEntry(ze);
        } else {
            zos.putNextEntry(new ZipEntry(resolvedPartUri));
        }

        zos.write(bytes);

        // Complete the entry
        zos.closeEntry();

    } catch (Exception e) {
        throw new Docx4JException("Failed to put binary part", e);
    }

    log.info("success writing part: " + resolvedPartUri);

}

From source file:org.duracloud.common.util.IOUtil.java

/**
 * Adds the specified file to the zip output stream.
 *
 * @param file//from  w w  w.  j a  v a2s  .co  m
 * @param zipOs
 */
public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException {
    String fileName = file.getName();
    try (FileInputStream fos = new FileInputStream(file)) {
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipEntry.setSize(file.length());
        zipEntry.setTime(System.currentTimeMillis());
        zipOs.putNextEntry(zipEntry);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = fos.read(buf)) > 0) {
            zipOs.write(buf, 0, bytesRead);
        }
        zipOs.closeEntry();
        fos.close();
    }
}

From source file:org.eclipse.winery.repository.backend.filebased.FilebasedRepository.java

@Override
public void doDump(OutputStream out) throws IOException {
    final ZipOutputStream zout = new ZipOutputStream(out);
    final int cutLength = this.repositoryRoot.toString().length() + 1;

    Files.walkFileTree(this.repositoryRoot, new SimpleFileVisitor<Path>() {

        @Override/*from w ww  .  j av  a 2  s . c o m*/
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            if (dir.endsWith(".git")) {
                return FileVisitResult.SKIP_SUBTREE;
            } else {
                return FileVisitResult.CONTINUE;
            }
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            String name = file.toString().substring(cutLength);
            ZipEntry ze = new ZipEntry(name);
            try {
                ze.setTime(Files.getLastModifiedTime(file).toMillis());
                ze.setSize(Files.size(file));
                zout.putNextEntry(ze);
                Files.copy(file, zout);
                zout.closeEntry();
            } catch (IOException e) {
                FilebasedRepository.logger.debug(e.getMessage());
            }
            return FileVisitResult.CONTINUE;
        }
    });
    zout.close();
}

From source file:org.exoplatform.services.document.test.BaseStandaloneTest.java

/**
 * Replaces the first part of the content of the given entry that matches with each regular expressions
 * with the provided replacements directly into the target zip file.
 *///from   w  w w .j  a  v a2  s  .  c om
public void replaceFirstInZip(InputStream is, File targetZipFile, String entryName, String[] regExprs,
        String[] replacements) throws IOException {
    ZipInputStream inZip = null;
    ZipOutputStream outZip = null;
    try {
        inZip = new ZipInputStream(is);
        outZip = new ZipOutputStream(new FileOutputStream(targetZipFile));

        for (ZipEntry in; (in = inZip.getNextEntry()) != null;) {
            ZipEntry out = new ZipEntry(in.getName());
            outZip.putNextEntry(out);
            if (in.getName().equals(entryName)) {
                String fileContent = IOUtils.toString(inZip);
                for (int i = 0; i < regExprs.length; i++) {
                    fileContent = fileContent.replaceFirst(regExprs[i], replacements[i]);
                }
                out.setSize(fileContent.length());
                IOUtils.write(fileContent, outZip);
            } else {
                IOUtils.copy(inZip, outZip);
            }
            outZip.closeEntry();
        }
    } finally {
        IOUtils.closeQuietly(inZip);
        IOUtils.closeQuietly(outZip);
    }
}

From source file:org.exoplatform.services.jcr.ext.artifact.ArtifactManagingServiceImpl.java

private void mapRepositoryToZipStream(Node parentNode, ZipOutputStream zout)
        throws RepositoryException, IOException {

    NodeIterator folderIterator = parentNode.getNodes();
    while (folderIterator.hasNext()) {
        Node folder = folderIterator.nextNode();
        if (folder.isNodeType("exo:artifact")) {
            String entryName = parentNode.getPath() + File.separator + folder.getName();
            ZipEntry entry = new ZipEntry(entryName + "/");

            zout.putNextEntry(entry);/* w ww . j  a  va  2 s.  c  o  m*/

            mapRepositoryToZipStream(folder, zout);

        } else if (folder.isNodeType("exo:file")) {

            String entryName = parentNode.getPath() + File.separator + folder.getName();
            ZipEntry entry = new ZipEntry(entryName);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Zipping " + entryName);
            }

            Node dataNode = folder.getNode("jcr:content");
            Property data = dataNode.getProperty("jcr:data");
            InputStream in = data.getStream();

            Property lastModified = dataNode.getProperty("jcr:lastModified");

            entry.setTime(lastModified.getLong());
            entry.setSize(in.available());
            zout.putNextEntry(entry);

            int count;
            byte[] buf = new byte[BUFFER];
            while ((count = in.read(buf, 0, BUFFER)) != -1) {
                zout.write(buf, 0, count);
            }
            zout.flush();
            in.close();
        }
    }
}

From source file:org.flowable.app.service.editor.AppDefinitionPublishService.java

protected byte[] createDeployZipArtifact(Map<String, byte[]> deployableAssets) {
    ByteArrayOutputStream baos = null;
    ZipOutputStream zos = null;/*from w  w  w. j  a  v a 2 s . c o m*/
    byte[] deployZipArtifact = null;
    try {
        baos = new ByteArrayOutputStream();
        zos = new ZipOutputStream(baos);

        for (Map.Entry<String, byte[]> entry : deployableAssets.entrySet()) {
            ZipEntry zipEntry = new ZipEntry(entry.getKey());
            zipEntry.setSize(entry.getValue().length);
            zos.putNextEntry(zipEntry);
            zos.write(entry.getValue());
            zos.closeEntry();
        }

        // this is the zip file as byte[]
        deployZipArtifact = baos.toByteArray();
    } catch (IOException ioe) {
        logger.error("Error adding deploy zip entry", ioe);
        throw new InternalServerErrorException("Could not create deploy zip artifact");
    }

    return deployZipArtifact;
}

From source file:org.flowable.app.service.editor.BaseAppDefinitionService.java

protected byte[] createDeployZipArtifact(Map<String, byte[]> deployableAssets) {
    ByteArrayOutputStream baos = null;
    ZipOutputStream zos = null;/*from  w w  w  .java 2 s.  c o  m*/
    byte[] deployZipArtifact = null;
    try {
        baos = new ByteArrayOutputStream();
        zos = new ZipOutputStream(baos);

        for (Map.Entry<String, byte[]> entry : deployableAssets.entrySet()) {
            ZipEntry zipEntry = new ZipEntry(entry.getKey());
            zipEntry.setSize(entry.getValue().length);
            zos.putNextEntry(zipEntry);
            zos.write(entry.getValue());
            zos.closeEntry();
        }

        IOUtils.closeQuietly(zos);

        // this is the zip file as byte[]
        deployZipArtifact = baos.toByteArray();

        IOUtils.closeQuietly(baos);

    } catch (IOException ioe) {
        throw new InternalServerErrorException("Could not create deploy zip artifact");
    }

    return deployZipArtifact;
}

From source file:org.flowable.ui.modeler.service.BaseAppDefinitionService.java

protected byte[] createDeployZipArtifact(Map<String, byte[]> deployableAssets) {

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(baos)) {
        for (Map.Entry<String, byte[]> entry : deployableAssets.entrySet()) {
            ZipEntry zipEntry = new ZipEntry(entry.getKey());
            zipEntry.setSize(entry.getValue().length);
            zos.putNextEntry(zipEntry);//from w  w  w .  ja  v a 2 s  .  c om
            zos.write(entry.getValue());
            zos.closeEntry();
        }

        // this is the zip file as byte[]
        return baos.toByteArray();

    } catch (IOException ioe) {
        throw new InternalServerErrorException("Could not create deploy zip artifact");
    }
}

From source file:org.gbif.occurrence.download.oozie.ArchiveBuilder.java

/**
 * Appends the compressed files found within the directory to the zip stream as the named file
 *//*from w  w w  .  j  ava  2 s .c o  m*/
private void appendPreCompressedFile(ModalZipOutputStream out, Path dir, String filename, String headerRow)
        throws IOException {
    RemoteIterator<LocatedFileStatus> files = hdfs.listFiles(dir, false);
    List<InputStream> parts = Lists.newArrayList();

    // Add the header first, which must also be compressed
    ByteArrayOutputStream header = new ByteArrayOutputStream();
    D2Utils.compress(new ByteArrayInputStream(headerRow.getBytes()), header);
    parts.add(new ByteArrayInputStream(header.toByteArray()));

    // Locate the streams to the compressed content on HDFS
    while (files.hasNext()) {
        LocatedFileStatus fs = files.next();
        Path path = fs.getPath();
        if (path.toString().endsWith(D2Utils.FILE_EXTENSION)) {
            LOG.info("Deflated content to merge: " + path);
            parts.add(hdfs.open(path));
        }
    }

    // create the Zip entry, and write the compressed bytes
    org.gbif.hadoop.compress.d2.zip.ZipEntry ze = new org.gbif.hadoop.compress.d2.zip.ZipEntry(filename);
    out.putNextEntry(ze, ModalZipOutputStream.MODE.PRE_DEFLATED);
    try (D2CombineInputStream in = new D2CombineInputStream(parts)) {
        ByteStreams.copy(in, out);
        in.close(); // important so counts are accurate
        ze.setSize(in.getUncompressedLength()); // important to set the sizes and CRC
        ze.setCompressedSize(in.getCompressedLength());
        ze.setCrc(in.getCrc32());
    } finally {
        out.closeEntry();
    }
}

From source file:org.infoscoop.service.GadgetResourceService.java

private static void popResource(Map<String, Object> tree, String path, ZipOutputStream zout)
        throws IOException {
    if (path.equals("/"))
        path = "";

    for (String key : tree.keySet()) {
        Object value = tree.get(key);
        if (key.startsWith("#"))
            key = key.substring(1);/*  www  .  j  a v a2  s .c  om*/

        ZipEntry entry = new ZipEntry(path + key);
        if (value instanceof Map) {
            entry.setMethod(ZipEntry.STORED);
            entry.setSize(0);
            entry.setCrc(0);

            //            zout.putNextEntry( entry );
            popResource((Map<String, Object>) value, path + key + "/", zout);
            //            zout.closeEntry();
        } else {
            byte[] data = (byte[]) tree.get(key);
            entry.setSize(data.length);

            zout.putNextEntry(entry);
            zout.write(data);
            zout.closeEntry();
        }
    }
}