Example usage for java.util.zip CheckedOutputStream close

List of usage examples for java.util.zip CheckedOutputStream close

Introduction

In this page you can find the example usage for java.util.zip CheckedOutputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:com.guye.baffle.decoder.ArscData.java

public CRC32 createObfuscateFile(StringBlock tableBlock, StringBlock keyBlock, File file) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    CRC32 cksum = new CRC32();
    CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fileOutputStream, cksum);
    LEDataOutputStream out = new LEDataOutputStream(checkedOutputStream);

    int tableStrChange = getmTableStrings().getSize() - tableBlock.getSize();
    int keyStrChange = getmSpecNames().getSize() - keyBlock.getSize();
    getmHeader().chunkSize -= (tableStrChange + keyStrChange);
    getmHeader().write(out);/*from   www.  j a v  a2  s. c o m*/
    out.writeInt(1);
    tableBlock.write(out);
    getmPkgHeader().header.chunkSize -= keyStrChange;
    getmPkgHeader().write(out);
    getTypeNames().write(out);
    keyBlock.write(out);

    byte[] buff = new byte[1024];
    FileInputStream in = new FileInputStream(getFile());
    in.skip(getmResIndex());
    int len;
    while (((len = in.read(buff)) != -1)) {
        out.write(buff, 0, len);
    }

    in.close();
    out.close();
    checkedOutputStream.close();
    fileOutputStream.close();
    return cksum;
}

From source file:com.atolcd.web.scripts.ZipContents.java

public void createZipFile(List<String> nodeIds, OutputStream os, boolean noaccent) throws IOException {
    File zip = null;/*from   w  w w .  j  a  va2  s  .c o  m*/

    try {
        if (nodeIds != null && !nodeIds.isEmpty()) {
            zip = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ZIP_EXTENSION);
            FileOutputStream stream = new FileOutputStream(zip);
            CheckedOutputStream checksum = new CheckedOutputStream(stream, new Adler32());
            BufferedOutputStream buff = new BufferedOutputStream(checksum);
            ZipArchiveOutputStream out = new ZipArchiveOutputStream(buff);
            out.setEncoding(encoding);
            out.setMethod(ZipArchiveOutputStream.DEFLATED);
            out.setLevel(Deflater.BEST_COMPRESSION);

            if (logger.isDebugEnabled()) {
                logger.debug("Using encoding '" + encoding + "' for zip file.");
            }

            try {
                for (String nodeId : nodeIds) {
                    NodeRef node = new NodeRef(storeRef, nodeId);
                    addToZip(node, out, noaccent, "");
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            } finally {
                out.close();
                buff.close();
                checksum.close();
                stream.close();

                if (nodeIds.size() > 0) {
                    InputStream in = new FileInputStream(zip);
                    try {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        int len;

                        while ((len = in.read(buffer)) > 0) {
                            os.write(buffer, 0, len);
                        }
                    } finally {
                        IOUtils.closeQuietly(in);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    } finally {
        // try and delete the temporary file
        if (zip != null) {
            zip.delete();
        }
    }
}

From source file:fr.itldev.koya.alfservice.KoyaContentService.java

/**
 *
 * @param nodeRefs/*from  w ww  .j a v a 2  s  . com*/
 * @return
 * @throws KoyaServiceException
 */
public File zip(List<String> nodeRefs) throws KoyaServiceException {
    File tmpZipFile = null;
    try {
        tmpZipFile = TempFileProvider.createTempFile("tmpDL", ".zip");
        FileOutputStream fos = new FileOutputStream(tmpZipFile);
        CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
        BufferedOutputStream buff = new BufferedOutputStream(checksum);
        ZipArchiveOutputStream zipStream = new ZipArchiveOutputStream(buff);
        // NOTE: This encoding allows us to workaround bug...
        // http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
        zipStream.setEncoding("UTF-8");

        zipStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        zipStream.setLevel(Deflater.BEST_COMPRESSION);

        zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
        zipStream.setUseLanguageEncodingFlag(true);
        zipStream.setFallbackToUTF8(true);

        try {
            for (String nodeRef : nodeRefs) {
                addToZip(koyaNodeService.getNodeRef(nodeRef), zipStream, "");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        } finally {
            zipStream.close();
            buff.close();
            checksum.close();
            fos.close();

        }
    } catch (IOException | WebScriptException e) {
        logger.error(e.getMessage(), e);
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    }

    return tmpZipFile;
}

From source file:org.theospi.portfolio.presentation.export.PresentationExport.java

public void createZip(OutputStream out) throws IOException {
    File directory = new File(tempDirectory + webappName);

    CheckedOutputStream checksum = null;
    ZipOutputStream zos = null;/* ww  w.  jav a 2  s.  c o  m*/
    try {
        checksum = new CheckedOutputStream(out, new Adler32());
        zos = new ZipOutputStream(new BufferedOutputStream(checksum));
        recurseDirectory("", directory, zos);

        zos.finish();
        zos.flush();
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
            }
        }
        if (checksum != null) {
            try {
                checksum.close();
            } catch (IOException e) {
            }
        }
    }

}