Example usage for java.util.zip ZipOutputStream STORED

List of usage examples for java.util.zip ZipOutputStream STORED

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream STORED.

Prototype

int STORED

To view the source code for java.util.zip ZipOutputStream STORED.

Click Source Link

Document

Compression method for uncompressed (STORED) entries.

Usage

From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java

void compressResults(final OutputStream out) throws IOException {
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(out)) {
        zipOutputStream.setLevel(ZipOutputStream.STORED);
        addToZipArchive(zipOutputStream, "participants.csv", getParticipantsCsv());
        addToZipArchive(zipOutputStream, "screenviews.csv", getScreenDataCsv());
        addToZipArchive(zipOutputStream, "tagdata.csv", getTagDataCsv());
        addToZipArchive(zipOutputStream, "tagpairdata.csv", getTagPairDataCsv());
        addToZipArchive(zipOutputStream, "timestampdata.csv", getTimeStampDataCsv());
        //            addToZipArchive(zipOutputStream, "groupdata.csv", getTimeStampDataCsv());
    }//w  w  w  .  j av a 2 s .c o m
}

From source file:org.apache.tika.server.writer.ZipWriter.java

private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
    zipEntry.setMethod(ZipOutputStream.STORED);

    zipEntry.setSize(dataBuffer.length);
    CRC32 crc32 = new CRC32();
    crc32.update(dataBuffer);//from   ww w.j  a  v a2s  . c  o m
    zipEntry.setCrc(crc32.getValue());

    try {
        zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
    } catch (ZipException ex) {
        if (name != null) {
            zipStoreBuffer(zip, "x-" + name, dataBuffer);
            return;
        }
    }

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

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 va2  s.c o  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.exist.xquery.modules.compression.AbstractCompressFunction.java

/**
 * Adds a element to a archive//from w  w w  .  j av  a2 s  . c o m
 *
 * @param os
 *            The Output Stream to add the element to
 * @param file
 *            The file to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressFile(OutputStream os, File file, boolean useHierarchy, String stripOffset, String method,
        String name) throws IOException {

    if (file.isFile()) {

        // create an entry in the Tar for the document
        Object entry = null;
        byte[] value = new byte[0];
        CRC32 chksum = new CRC32();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        if (name != null) {
            entry = newEntry(name);
        } else if (useHierarchy) {
            entry = newEntry(removeLeadingOffset(file.getPath(), stripOffset));
        } else {
            entry = newEntry(file.getName());
        }

        InputStream is = new FileInputStream(file);
        byte[] data = new byte[16384];
        int len = 0;
        while ((len = is.read(data, 0, data.length)) > 0) {
            baos.write(data, 0, len);
        }
        is.close();
        value = baos.toByteArray();
        // close the entry
        if (entry instanceof ZipEntry && "store".equals(method)) {
            ((ZipEntry) entry).setMethod(ZipOutputStream.STORED);
            chksum.update(value);
            ((ZipEntry) entry).setCrc(chksum.getValue());
            ((ZipEntry) entry).setSize(value.length);
        }

        putEntry(os, entry);
        os.write(value);
        closeEntry(os);

    } else {

        for (String i : file.list()) {
            compressFile(os, new File(file, i), useHierarchy, stripOffset, method, null);
        }

    }

}

From source file:org.exist.xquery.modules.compression.AbstractCompressFunction.java

/**
* Adds a element to a archive/*from w ww.  j a v a 2 s.  c  o  m*/
* 
* @param os
*            The Output Stream to add the element to
* @param element
*            The element to add to the archive
* @param useHierarchy
*            Whether to use a folder hierarchy in the archive file that
*            reflects the collection hierarchy
*/
private void compressElement(OutputStream os, Element element, boolean useHierarchy, String stripOffset)
        throws XPathException {

    if (!(element.getNodeName().equals("entry") || element.getNamespaceURI().length() > 0))
        throw new XPathException(this, "Item must be type of xs:anyURI or element entry.");

    if (element.getChildNodes().getLength() > 1)
        throw new XPathException(this, "Entry content is not valid XML fragment.");

    String name = element.getAttribute("name");
    //            if(name == null)
    //                throw new XPathException(this, "Entry must have name attribute.");

    String type = element.getAttribute("type");

    if ("uri".equals(type)) {
        compressFromUri(os, URI.create(element.getFirstChild().getNodeValue()), useHierarchy, stripOffset,
                element.getAttribute("method"), name);
        return;
    }

    if (useHierarchy) {
        name = removeLeadingOffset(name, stripOffset);
    } else {
        name = name.substring(name.lastIndexOf("/") + 1);
    }

    if ("collection".equals(type))
        name += "/";

    Object entry = null;

    try {

        entry = newEntry(name);

        if (!"collection".equals(type)) {
            byte[] value;
            CRC32 chksum = new CRC32();
            Node content = element.getFirstChild();

            if (content == null) {
                value = new byte[0];
            } else {
                if (content.getNodeType() == Node.TEXT_NODE) {
                    String text = content.getNodeValue();
                    Base64Decoder dec = new Base64Decoder();
                    if ("binary".equals(type)) {
                        //base64 binary
                        dec.translate(text);
                        value = dec.getByteArray();
                    } else {
                        //text
                        value = text.getBytes();
                    }
                } else {
                    //xml
                    Serializer serializer = context.getBroker().getSerializer();
                    serializer.setUser(context.getUser());
                    serializer.setProperty("omit-xml-declaration", "no");
                    getDynamicSerializerOptions(serializer);
                    value = serializer.serialize((NodeValue) content).getBytes();
                }
            }

            if (entry instanceof ZipEntry && "store".equals(element.getAttribute("method"))) {
                ((ZipEntry) entry).setMethod(ZipOutputStream.STORED);
                chksum.update(value);
                ((ZipEntry) entry).setCrc(chksum.getValue());
                ((ZipEntry) entry).setSize(value.length);
            }
            putEntry(os, entry);

            os.write(value);
        }
    } catch (IOException ioe) {
        throw new XPathException(this, ioe.getMessage(), ioe);
    } catch (SAXException saxe) {
        throw new XPathException(this, saxe.getMessage(), saxe);
    } finally {
        if (entry != null)
            try {
                closeEntry(os);
            } catch (IOException ioe) {
                throw new XPathException(this, ioe.getMessage(), ioe);
            }
    }
}

From source file:org.exist.xquery.modules.compression.AbstractCompressFunction.java

/**
 * Adds a document to a archive//  www . j  av a  2 s .  c  o  m
 * 
 * @param os
 *            The Output Stream to add the document to
 * @param doc
 *            The document to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressResource(OutputStream os, DocumentImpl doc, boolean useHierarchy, String stripOffset,
        String method, String name) throws IOException, SAXException {
    // create an entry in the Tar for the document
    Object entry = null;
    byte[] value = new byte[0];
    CRC32 chksum = new CRC32();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    if (name != null) {
        entry = newEntry(name);
    } else if (useHierarchy) {
        String docCollection = doc.getCollection().getURI().toString();
        XmldbURI collection = XmldbURI.create(removeLeadingOffset(docCollection, stripOffset));
        entry = newEntry(collection.append(doc.getFileURI()).toString());
    } else {
        entry = newEntry(doc.getFileURI().toString());
    }

    if (doc.getResourceType() == DocumentImpl.XML_FILE) {
        // xml file
        Serializer serializer = context.getBroker().getSerializer();
        serializer.setUser(context.getUser());
        serializer.setProperty("omit-xml-declaration", "no");
        getDynamicSerializerOptions(serializer);
        String strDoc = serializer.serialize(doc);
        value = strDoc.getBytes();
    } else if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
        // binary file
        InputStream is = context.getBroker().getBinaryResource((BinaryDocument) doc);
        byte[] data = new byte[16384];
        int len = 0;
        while ((len = is.read(data, 0, data.length)) > 0) {
            baos.write(data, 0, len);
        }
        is.close();
        value = baos.toByteArray();
    }
    // close the entry
    if (entry instanceof ZipEntry && "store".equals(method)) {
        ((ZipEntry) entry).setMethod(ZipOutputStream.STORED);
        chksum.update(value);
        ((ZipEntry) entry).setCrc(chksum.getValue());
        ((ZipEntry) entry).setSize(value.length);
    }

    putEntry(os, entry);
    os.write(value);
    closeEntry(os);
}

From source file:org.geoserver.wps.ppio.ZipArchivePPIO.java

/**
 * Instantiates a new zip archive ppio./*w w w  .  j a v  a  2s  .c om*/
 * 
 * @param resources the resources
 */
public ZipArchivePPIO(int compressionLevel) {
    super(File.class, File.class, "application/zip");
    if (compressionLevel < ZipOutputStream.STORED || compressionLevel > ZipOutputStream.DEFLATED) {
        throw new IllegalArgumentException("Invalid Compression Level: " + compressionLevel);
    }
    this.compressionLevel = compressionLevel;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Using compression level " + compressionLevel);
    }
}

From source file:org.geoserver.wps.ppio.ZipArchivePPIO.java

/**
 * Default constructor using ZipOutputStream.STORED compression level.
 * //from   w w w.  jav  a2 s  .c  o  m
 */
public ZipArchivePPIO() {
    this(ZipOutputStream.STORED);
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, "Using compression level " + ZipOutputStream.STORED);
    }
}

From source file:org.lockss.exporter.ZipExporter.java

private void ensureOpenZip() throws IOException {
    if (zip != null) {
        if (getMaxSize() <= 0 || cos.getByteCount() < getMaxSize()) {
            return;
        }/* www. j  a v  a2s.  com*/
        closeZip();
    }

    if (timestamp == null) {
        timestamp = ArchiveUtils.get14DigitDate();
    }
    String name = prefix + "-" + timestamp + "-" + serialNumberFormatter.format(serialNo++) + ".zip";
    File file = new File(getDir(), name);
    fos = new FileOutputStream(file);
    cos = new CountingOutputStream(fos);
    OutputStream os = new BufferedOutputStream(cos);
    zip = new ZipOutputStream(os);
    if (!compress) {
        zip.setLevel(ZipOutputStream.STORED);
    }
    setZipComment(zip);
}

From source file:org.openmicroscopy.shoola.util.file.IOUtil.java

/**
 * Makes the zip.//  w w w .ja  va  2 s .  com
 * 
 * @param zipName The name of the zip.
 * @param compress Pass <code>true</code> to compress,
 * <code>false</code> otherwise.
 */
public static File zipDirectory(File zip, boolean compress) throws Exception {
    if (zip == null)
        throw new IllegalArgumentException("No name specified.");
    if (!zip.isDirectory() || !zip.exists())
        throw new IllegalArgumentException("Not a valid directory.");
    //Check if the name already has the extension
    String extension = FilenameUtils.getExtension(zip.getName());
    String name = zip.getName();
    if (StringUtils.isEmpty(extension) || !ZIP_EXTENSION.equals("." + extension)) {
        name += ZIP_EXTENSION;
    }
    File file = new File(zip.getParentFile(), name);
    ZipOutputStream out = null;
    try {
        out = new ZipOutputStream(new FileOutputStream(file));
        if (!compress)
            out.setLevel(ZipOutputStream.STORED);
        zipDir(zip, out, null);
    } catch (Exception e) {
        throw new Exception("Cannot create the zip.", e);
    } finally {
        if (out != null)
            out.close();
    }
    return file;
}