Example usage for java.util.zip ZipOutputStream setLevel

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

Introduction

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

Prototype

public void setLevel(int level) 

Source Link

Document

Sets the compression level for subsequent entries which are DEFLATED.

Usage

From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java

/**
 * Compress data/*from ww  w  .  j a  va2  s . co m*/
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(baos);
    ZipOutputStream zos = new ZipOutputStream(cos);
    try {
        zos.setLevel(fileCompressor.getLevel().getValue());
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setComment(fileCompressor.getComment());
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            zos.putNextEntry(new ZipEntry(binaryFile.getDesPath()));
            zos.write(binaryFile.getData());
            zos.closeEntry();
        }
        zos.flush();
        zos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        zos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.espringtran.compressor4j.processor.GzipProcessor.java

/**
 * Compress data//  w w w .j a va2s  .  c  om
 * 
 * @param fileCompressor
 *            FileCompressor object
 * @return
 * @throws Exception
 */
@Override
public byte[] compressData(FileCompressor fileCompressor) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GzipCompressorOutputStream cos = new GzipCompressorOutputStream(baos);
    ZipOutputStream zos = new ZipOutputStream(cos);
    try {
        zos.setLevel(fileCompressor.getLevel().getValue());
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setComment(fileCompressor.getComment());
        for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) {
            zos.putNextEntry(new ZipEntry(binaryFile.getDesPath()));
            zos.write(binaryFile.getData());
            zos.closeEntry();
        }
        zos.flush();
        zos.finish();
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on compress data", e);
    } finally {
        zos.close();
        cos.close();
        baos.close();
    }
    return baos.toByteArray();
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

public String toBase64() {
    try {//from  w w w  . j a  v a  2s .c o m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ZipOutputStream zip = new ZipOutputStream(bos);
            try {
                zip.setLevel(9);
                zip.putNextEntry(new ZipEntry("entry"));
                try {
                    ObjectOutput out = new ObjectOutputStream(zip);
                    try {
                        out.writeObject(this);
                    } finally {
                        out.flush();
                    }
                } finally {
                    zip.closeEntry();
                }
                zip.flush();
                return Base64.encodeBase64URLSafeString(bos.toByteArray());
            } finally {
                zip.close();
            }
        } finally {
            bos.close();
        }
    } catch (IOException e) {
        log.log((Level.SEVERE), "IO Error while serializing member", e);
        return null;
    }
}

From source file:org.olat.modules.qpool.manager.ExportQItemResource.java

@Override
public void prepare(HttpServletResponse hres) {
    try {//from   w ww  .  ja va 2  s  .c  o m
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }

    String label = item.getTitle();
    String file = StringHelper.transformDisplayNameToFileSystemName(label) + ".zip";
    String encodedFileName = StringHelper.urlEncodeUTF8(file);
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
    hres.setHeader("Content-Description", encodedFileName);

    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(hres.getOutputStream());
        zout.setLevel(9);
        Set<String> names = new HashSet<String>();
        QPoolService qpoolService = CoreSpringFactory.getImpl(QPoolService.class);
        qpoolService.exportItem(item, zout, names);
    } catch (IOException e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(zout);
    }
}

From source file:org.olat.modules.qpool.manager.AbstractExportTestResource.java

@Override
public void prepare(HttpServletResponse hres) {
    try {//from  www. j a  v a  2s  . co  m
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }

    String label = "Test";
    String file = StringHelper.transformDisplayNameToFileSystemName(label) + ".zip";
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + StringHelper.urlEncodeUTF8(file));
    hres.setHeader("Content-Description", StringHelper.urlEncodeUTF8(label));

    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(hres.getOutputStream());
        zout.setLevel(9);
        exportTest(items, zout);
    } catch (IOException e) {
        log.error("", e);
    } catch (Exception e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(zout);
    }
}

From source file:org.olat.modules.qpool.manager.ExportQItemsZipResource.java

@Override
public void prepare(HttpServletResponse hres) {
    try {//ww w.j a  v a 2  s .  com
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }

    String label = "ExportItems";
    String file = StringHelper.transformDisplayNameToFileSystemName(label) + ".zip";
    String encodedFileName = StringHelper.urlEncodeUTF8(file);
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
    hres.setHeader("Content-Description", encodedFileName);

    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(hres.getOutputStream());
        zout.setLevel(9);
        Set<String> names = new HashSet<String>();
        QPoolService qpoolService = CoreSpringFactory.getImpl(QPoolService.class);
        for (QuestionItemFull item : items) {
            qpoolService.exportItem(item, zout, names);
        }
    } catch (IOException e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(zout);
    }
}

From source file:org.olat.course.archiver.ArchiveResource.java

@Override
public void prepare(HttpServletResponse hres) {
    try {//from  w ww  . ja  v  a2 s .c o m
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }

    String label = courseNode.getType() + "_"
            + StringHelper.transformDisplayNameToFileSystemName(courseNode.getShortName()) + "_"
            + Formatter.formatDatetimeFilesystemSave(new Date(System.currentTimeMillis())) + ".zip";
    String urlEncodedLabel = StringHelper.urlEncodeUTF8(label);
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + urlEncodedLabel);
    hres.setHeader("Content-Description", urlEncodedLabel);

    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(hres.getOutputStream());
        zout.setLevel(9);
        ICourse course = CourseFactory.loadCourse(courseOres);
        courseNode.archiveNodeData(locale, course, options, zout, encoding);
    } catch (Exception e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(zout);
    }
}

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  ww  w .j a  va  2s. c om
    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;
}

From source file:br.univali.celine.lms.utils.zip.Zip.java

public void zip(ArrayList<String> fileList, File destFile, int compressionLevel) throws Exception {

    if (destFile.exists())
        throw new Exception("File " + destFile.getName() + " already exists!!");

    int fileLength;
    byte[] buffer = new byte[4096];

    FileOutputStream fos = new FileOutputStream(destFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zipFile = new ZipOutputStream(bos);
    zipFile.setLevel(compressionLevel);

    for (int i = 0; i < fileList.size(); i++) {

        FileInputStream fis = new FileInputStream(fileList.get(i));
        BufferedInputStream bis = new BufferedInputStream(fis);
        ZipEntry ze = new ZipEntry(FilenameUtils.getName(fileList.get(i)));
        zipFile.putNextEntry(ze);/*from   www .jav  a 2  s . c om*/

        while ((fileLength = bis.read(buffer, 0, 4096)) > 0)
            zipFile.write(buffer, 0, fileLength);

        zipFile.closeEntry();
        bis.close();
    }

    zipFile.close();

}

From source file:org.olat.modules.wiki.WikiToCPResource.java

@Override
public void prepare(HttpServletResponse hres) {
    try {// www.j a  v a 2 s  .c o  m
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }

    Wiki wiki = WikiManager.getInstance().getOrLoadWiki(ores);
    String label = "Wiki";
    String urlEncodedLabel = StringHelper.urlEncodeUTF8(label);
    hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + urlEncodedLabel);
    hres.setHeader("Content-Description", urlEncodedLabel);

    ZipOutputStream zout = null;
    try {
        zout = new ZipOutputStream(hres.getOutputStream());
        zout.setLevel(9);
        wikiToCP(wiki, zout);
    } catch (Exception e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(zout);
    }
}