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:se.altrusoft.docserv.odsprocessor.ODSProcessor.java

public static void transformODS(InputStream inputStream, OutputStream outputStream,
        DOMTransformer contentTransformer) {
    ZipInputStream zipInputStream = null;
    ZipOutputStream zipOutputStream = null;
    try {//from  ww w. j a v a  2  s  .c  om
        zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
        zipOutputStream = new ZipOutputStream(outputStream);

        for (ZipEntry zipInputEntry; (zipInputEntry = zipInputStream.getNextEntry()) != null;) {
            ZipEntry zipOutputEntry = null;
            InputStream zipEntrySource = null;

            String inputEntryName = zipInputEntry.getName();

            if (inputEntryName.equals(CONTENT_FILE)) {

                // Found Content File

                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

                // Build a DOM from the original content found in
                // 'zipInputStream'
                // There is a bug in dBuilder.parse() that will close
                // the input stream - this is why we have to extract all
                // content file data
                // from 'zipInputStream' in a temporary byte[] and wrap it
                // in a new input
                // stream...

                Document domdoc = dBuilder.parse(new ByteArrayInputStream(IOUtils.toByteArray(zipInputStream)));
                domdoc.getDocumentElement().normalize();

                // Perform transformation of DOM:
                contentTransformer.transform(domdoc);

                // Finally serialize the DOM now containing transformed
                // content.

                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource domSource = new DOMSource(domdoc);

                ByteArrayOutputStream transformedContent = new ByteArrayOutputStream();
                StreamResult result = new StreamResult(transformedContent);
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.transform(domSource, result);

                byte transformedContentBytes[] = transformedContent.toByteArray();
                zipOutputEntry = new ZipEntry(zipInputEntry.getName());
                zipOutputEntry.setSize(transformedContentBytes.length);
                zipEntrySource = new ByteArrayInputStream(transformedContentBytes);

            } else {
                zipOutputEntry = zipInputEntry;
                zipEntrySource = zipInputStream;
            }
            zipOutputStream.putNextEntry(zipOutputEntry);
            IOUtils.copy(zipEntrySource, zipOutputStream);
        }
    } catch (IOException e) {
        throw new Error("Unable to transform ODS", e);
    } catch (ParserConfigurationException e) {
        throw new Error("Unable to transform ODS", e);
    } catch (SAXException e) {
        throw new Error("Unable to transform ODS", e);
    } catch (TransformerConfigurationException e) {
        throw new Error("Unable to transform ODS", e);
    } catch (TransformerException e) {
        throw new Error("Unable to transform ODS", e);
    } finally {
        IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(zipOutputStream);
    }
}

From source file:sk.baka.aedict.indexer.Main.java

private void zipLuceneIndex() throws IOException {
    System.out.println("Zipping the index file");
    final File zip = new File(config.getTargetFileName());
    if (zip.exists() && !zip.delete()) {
        throw new IOException("Cannot delete " + zip.getAbsolutePath());
    }//w w  w .j  a v a 2s  . c o m
    final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
    try {
        out.setLevel(9);
        final File[] luceneIndexFiles = new File(LUCENE_INDEX).listFiles();
        for (final File indexFile : luceneIndexFiles) {
            final ZipEntry entry = new ZipEntry(indexFile.getName());
            entry.setSize(indexFile.length());
            out.putNextEntry(entry);
            final InputStream in = new FileInputStream(indexFile);
            try {
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
            }
            out.closeEntry();
        }
    } finally {
        IOUtils.closeQuietly(out);
    }
    System.out.println("Finished index zipping");
}

From source file:util.FileArchiver.java

private void addFileToZip(ZipOutputStream os, FileAccess input, boolean gzipFiles) throws IOException {
    // Gzip input file first
    FileAccess output = input;// ww  w .j  ava 2  s .c o m
    if (gzipFiles) {
        output = gzipFile(input);
    }

    // Determine zip entry name
    LOG.debug("Base path(" + output.basePath.toString() + ")");
    LOG.debug("Output path(" + output.path.toString() + ")");
    //String entryName = output.path.getName();
    String entryName = output.path.toString().replaceFirst(output.basePath.toString(), "");
    if (entryName.startsWith("/")) {
        entryName = entryName.substring(1);
    }
    LOG.info("....creating archive entry (" + entryName + ")");
    ZipEntry entry = new ZipEntry(entryName);

    FileStatus status = output.fs.getFileStatus(output.path);
    LOG.info("......setting size to (" + status.getLen() + ")");
    try {
        entry.setSize(status.getLen());
    } catch (Exception e) {
        LOG.error(e.toString());
    }

    // Create new entry in archive
    os.putNextEntry(entry);

    // Copy compressed version of file into entry
    LOG.info("......adding data from file (" + output.path + ")");
    InputStream is = null;
    try {
        is = output.fs.open(output.path);
        copyFile(is, os);
    } finally {
        IOUtils.closeQuietly(is);
    }
    LOG.info("......closing archive entry");
    // Close entry
    os.closeEntry();
}