Example usage for java.util.zip ZipEntry setCrc

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

Introduction

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

Prototype

public void setCrc(long crc) 

Source Link

Document

Sets the CRC-32 checksum of the uncompressed entry data.

Usage

From source file:org.digidoc4j.impl.bdoc.asic.AsicContainerCreator.java

private ZipEntry getAsicMimeTypeZipEntry(byte[] mimeTypeBytes) {
    ZipEntry entryMimetype = new ZipEntry(ZIP_ENTRY_MIMETYPE);
    entryMimetype.setMethod(ZipEntry.STORED);
    entryMimetype.setSize(mimeTypeBytes.length);
    entryMimetype.setCompressedSize(mimeTypeBytes.length);
    CRC32 crc = new CRC32();
    crc.update(mimeTypeBytes);//  w ww . j  a va  2 s  .c o m
    entryMimetype.setCrc(crc.getValue());
    return entryMimetype;
}

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   ww  w .  j  a v  a  2 s  .c om*/

        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.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 om
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);/*from w  ww  .ja  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();
        }
    }
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

private static void processFiles(final File packDir, final File file, final ZipOutputStream out,
        final IFileFilter filter) throws IOException {
    if (!filter.accept(file))
        return;// w  ww.j av  a2 s.  c  o m

    if (file.isDirectory()) {
        final ZipEntry e = new ZipEntry(convertFileName(packDir, file) + "/"); //$NON-NLS-1$
        out.putNextEntry(e);
        out.closeEntry();

        final File[] files = file.listFiles();
        for (final File f : files) {
            processFiles(packDir, f, out, filter);
        }
    } else {

        final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        final ZipEntry e = new ZipEntry(convertFileName(packDir, file));
        final CRC32 crc = new CRC32();

        out.putNextEntry(e);

        final byte[] buf = new byte[4096];
        int len = 0;

        while ((len = bis.read(buf)) > 0) {
            out.write(buf, 0, len);
            crc.update(buf, 0, len);
        }
        e.setCrc(crc.getValue());

        out.closeEntry();
        bis.close();

    }
}

From source file:org.kuali.kfs.module.ar.document.service.impl.DunningLetterServiceImpl.java

/**
 * This method generates the actual pdf files to print.
 *
 * @param mapping/*from  w  w  w . j  a  va2s . c  o  m*/
 * @param form
 * @param list
 * @return
 */
@Override
public boolean createZipOfPDFs(byte[] report, ByteArrayOutputStream baos) throws IOException {

    ZipOutputStream zos = new ZipOutputStream(baos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(report)) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(report));
        crc.reset();
        while ((bytesRead = bis.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
        }
        bis.close();
        // Reset to beginning of input stream
        bis = new BufferedInputStream(new ByteArrayInputStream(report));
        ZipEntry entry = new ZipEntry("DunningLetters&Invoices-"
                + getDateTimeService().toDateStringForFilename(getDateTimeService().getCurrentDate()) + ".pdf");
        entry.setMethod(ZipEntry.STORED);
        entry.setCompressedSize(report.length);
        entry.setSize(report.length);
        entry.setCrc(crc.getValue());
        zos.putNextEntry(entry);
        while ((bytesRead = bis.read(buffer)) != -1) {
            zos.write(buffer, 0, bytesRead);
        }
        bis.close();
    }

    zos.close();
    return true;
}

From source file:org.kuali.kfs.module.ar.report.service.impl.TransmitContractsAndGrantsInvoicesServiceImpl.java

/**
 *
 * @param report/*from  w  ww. j  a va  2s  .c  om*/
 * @param invoiceFileWritten
 * @param zos
 * @param buffer
 * @param crc
 * @return
 * @throws IOException
 */
private boolean writeFile(byte[] arrayToWrite, ZipOutputStream zos, String fileName) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    if (ObjectUtils.isNotNull(arrayToWrite) && arrayToWrite.length > 0) {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
        try {
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }
            bis.close();
            // Reset to beginning of input stream
            bis = new BufferedInputStream(new ByteArrayInputStream(arrayToWrite));
            ZipEntry entry = new ZipEntry(fileName);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(arrayToWrite.length);
            entry.setSize(arrayToWrite.length);
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } finally {
            bis.close();
        }
        return true;
    }
    return false;
}

From source file:org.nuxeo.template.odt.OOoArchiveModifier.java

protected void writeOOoEntry(ZipOutputStream zipOutputStream, String entryName, File fileEntry, int zipMethod)
        throws IOException {

    if (fileEntry.isDirectory()) {
        entryName = entryName + "/";
        ZipEntry zentry = new ZipEntry(entryName);
        zipOutputStream.putNextEntry(zentry);
        zipOutputStream.closeEntry();//from  www. j  a  va  2s  .  c o  m
        for (File child : fileEntry.listFiles()) {
            writeOOoEntry(zipOutputStream, entryName + child.getName(), child, zipMethod);
        }
        return;
    }

    ZipEntry zipEntry = new ZipEntry(entryName);
    try (InputStream entryInputStream = new FileInputStream(fileEntry)) {
        zipEntry.setMethod(zipMethod);
        if (zipMethod == ZipEntry.STORED) {
            byte[] inputBytes = IOUtils.toByteArray(entryInputStream);
            CRC32 crc = new CRC32();
            crc.update(inputBytes);
            zipEntry.setCrc(crc.getValue());
            zipEntry.setSize(inputBytes.length);
            zipEntry.setCompressedSize(inputBytes.length);
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.write(inputBytes, zipOutputStream);
        } else {
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.copy(entryInputStream, zipOutputStream);
        }
    }
    zipOutputStream.closeEntry();
}

From source file:org.tangram.components.CodeExporter.java

@LinkAction("/codes.zip")
public TargetDescriptor codes(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!request.getRequestURI().endsWith(".zip")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    } // if/*from   w  w  w .  j  av a2 s. co  m*/
    if (request.getAttribute(Constants.ATTRIBUTE_ADMIN_USER) == null) {
        throw new IOException("User may not execute action");
    } // if

    long now = System.currentTimeMillis();

    response.setContentType("application/x-zip-compressed");

    CRC32 crc = new CRC32();

    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    zos.setComment("Tangram Repository Codes");
    zos.setLevel(9);
    Collection<CodeResource> codes = codeResourceCache.getCodes();
    for (CodeResource code : codes) {
        if (StringUtils.isNotBlank(code.getAnnotation())) {
            String mimeType = CodeHelper.getNormalizedMimeType(code.getMimeType());
            String folder = CodeHelper.getFolder(mimeType);
            String extension = CodeHelper.getExtension(mimeType);
            if (mimeType.startsWith("text/")) {
                byte[] bytes = code.getCodeText().getBytes("UTF-8");
                ZipEntry ze = new ZipEntry(folder + "/" + getFilename(code) + extension);
                ze.setTime(now);
                crc.reset();
                crc.update(bytes);
                ze.setCrc(crc.getValue());
                zos.putNextEntry(ze);
                zos.write(bytes);
                zos.closeEntry();
            } // if
        } // if
    } // for
    zos.finish();
    zos.close();

    return TargetDescriptor.DONE;
}