Example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setMethod

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setMethod

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveOutputStream setMethod.

Prototype

public void setMethod(int method) 

Source Link

Document

Sets the default compression method for subsequent entries.

Usage

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

public static void writeArchivedLogTailToStream(File logFile, OutputStream outputStream) throws IOException {
    if (!logFile.exists()) {
        throw new FileNotFoundException();
    }/*  w ww.  j av  a2s.c om*/

    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
    zipOutputStream.setEncoding(ZIP_ENCODING);

    byte[] content = getTailBytes(logFile);

    ArchiveEntry archiveEntry = newTailArchive(logFile.getName(), content);
    zipOutputStream.putArchiveEntry(archiveEntry);
    zipOutputStream.write(content);

    zipOutputStream.closeArchiveEntry();
    zipOutputStream.close();
}

From source file:com.haulmont.cuba.core.sys.logging.LogArchiver.java

public static void writeArchivedLogToStream(File logFile, OutputStream outputStream) throws IOException {
    if (!logFile.exists()) {
        throw new FileNotFoundException();
    }/*from w  w  w  .j a v  a  2  s .  c  o  m*/

    File tempFile = File.createTempFile(FilenameUtils.getBaseName(logFile.getName()) + "_log_", ".zip");

    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFile);
    zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
    zipOutputStream.setEncoding(ZIP_ENCODING);

    ArchiveEntry archiveEntry = newArchive(logFile);
    zipOutputStream.putArchiveEntry(archiveEntry);

    FileInputStream logFileInput = new FileInputStream(logFile);
    IOUtils.copyLarge(logFileInput, zipOutputStream);

    logFileInput.close();

    zipOutputStream.closeArchiveEntry();
    zipOutputStream.close();

    FileInputStream tempFileInput = new FileInputStream(tempFile);
    IOUtils.copyLarge(tempFileInput, outputStream);

    tempFileInput.close();

    FileUtils.deleteQuietly(tempFile);
}

From source file:es.ucm.fdi.util.archive.ZipFormat.java

public void create(ArrayList<File> sources, File destFile, File baseDir) throws IOException {

    // to avoid modifying input argument
    ArrayList<File> toAdd = new ArrayList<>(sources);
    ZipArchiveOutputStream zos = null;

    try {/*from  w  w w  . j a  v  a  2s  .co m*/
        zos = new ZipArchiveOutputStream(new FileOutputStream(destFile));
        zos.setMethod(ZipArchiveOutputStream.DEFLATED);
        byte[] b = new byte[1024];

        //log.debug("Creating zip file: "+ficheroZip.getName());
        for (int i = 0; i < toAdd.size(); i++) {
            // note: cannot use foreach because sources gets modified
            File file = toAdd.get(i);

            // zip standard uses fw slashes instead of backslashes, always
            String baseName = baseDir.getAbsolutePath() + '/';
            String fileName = file.getAbsolutePath().substring(baseName.length());
            if (file.isDirectory()) {
                fileName += '/';
            }
            ZipArchiveEntry entry = new ZipArchiveEntry(fileName);

            // skip directories - after assuring that their children *will* be included.
            if (file.isDirectory()) {
                //log.debug("\tAdding dir "+fileName);
                for (File child : file.listFiles()) {
                    toAdd.add(child);
                }
                zos.putArchiveEntry(entry);
                continue;
            }

            //log.debug("\tAdding file "+fileName);

            // Add the zip entry and associated data.
            zos.putArchiveEntry(entry);

            int n;
            try (FileInputStream fis = new FileInputStream(file)) {
                while ((n = fis.read(b)) > -1) {
                    zos.write(b, 0, n);
                }
                zos.closeArchiveEntry();
            }
        }
    } finally {
        if (zos != null) {
            zos.finish();
            zos.close();
        }
    }
}

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  ava 2  s .co 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:com.haulmont.cuba.core.app.FoldersServiceBean.java

@Override
public byte[] exportFolder(Folder folder) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
    String xml = createXStream().toXML(folder);
    byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
    ArchiveEntry zipEntryDesign = newStoredEntry("folder.xml", xmlBytes);
    zipOutputStream.putArchiveEntry(zipEntryDesign);
    zipOutputStream.write(xmlBytes);//from w  w  w  .j  a  va2 s . c o  m
    try {
        zipOutputStream.closeArchiveEntry();
    } catch (Exception ex) {
        throw new RuntimeException(
                String.format("Exception occurred while exporting folder %s.", folder.getName()));
    }

    zipOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}

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

/**
 *
 * @param nodeRefs//from   w  ww  .ja va 2  s. c o m
 * @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:com.haulmont.cuba.core.app.importexport.EntityImportExport.java

@Override
public byte[] exportEntitiesToZIP(Collection<? extends Entity> entities) {
    String json = entitySerialization.toJson(entities, null,
            EntitySerializationOption.COMPACT_REPEATED_ENTITIES);
    byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream);
    zipOutputStream.setMethod(ZipArchiveOutputStream.STORED);
    zipOutputStream.setEncoding(StandardCharsets.UTF_8.name());
    ArchiveEntry singleDesignEntry = newStoredEntry("entities.json", jsonBytes);
    try {/*from   w  w w. java 2s  .c o  m*/
        zipOutputStream.putArchiveEntry(singleDesignEntry);
        zipOutputStream.write(jsonBytes);
        zipOutputStream.closeArchiveEntry();
    } catch (Exception e) {
        throw new RuntimeException("Error on creating zip archive during entities export", e);
    } finally {
        IOUtils.closeQuietly(zipOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:no.difi.sdp.client.asice.archive.CreateZip.java

public Archive zipIt(List<AsicEAttachable> files) {
    ByteArrayOutputStream archive = null;
    ZipArchiveOutputStream zipOutputStream = null;
    try {//from w w  w .ja va 2 s .c o m
        archive = new ByteArrayOutputStream();
        zipOutputStream = new ZipArchiveOutputStream(archive);
        zipOutputStream.setEncoding(Charsets.UTF_8.name());
        zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        for (AsicEAttachable file : files) {
            log.trace("Adding " + file.getFileName() + " to archive. Size in bytes before compression: "
                    + file.getBytes().length);
            ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getFileName());
            zipEntry.setSize(file.getBytes().length);

            zipOutputStream.putArchiveEntry(zipEntry);
            IOUtils.copy(new ByteArrayInputStream(file.getBytes()), zipOutputStream);
            zipOutputStream.closeArchiveEntry();
        }
        zipOutputStream.finish();
        zipOutputStream.close();

        return new Archive(archive.toByteArray());
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    } finally {
        IOUtils.closeQuietly(archive);
        IOUtils.closeQuietly(zipOutputStream);
    }
}

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

public void writeTo(Map<String, byte[]> parts, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    ZipArchiveOutputStream zip = new ZipArchiveOutputStream(entityStream);

    zip.setMethod(ZipArchiveOutputStream.STORED);

    for (Map.Entry<String, byte[]> entry : parts.entrySet()) {
        zipStoreBuffer(zip, entry.getKey(), entry.getValue());
    }/*  w  w  w .  j ava  2 s  .  com*/

    zip.close();
}

From source file:org.dataconservancy.packaging.tool.impl.ZipArchiveStreamFactory.java

public ZipArchiveOutputStream newArchiveOutputStream(OutputStream out) {
    ZipArchiveOutputStream zipOs = new ZipArchiveOutputStream(out);
    zipOs.setEncoding(encoding);// w  w w  .  j ava2 s  . com
    zipOs.setFallbackToUTF8(fallbackToUTF8);
    zipOs.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
    zipOs.setLevel(level);
    zipOs.setMethod(method);
    zipOs.setUseZip64(useZip64);

    return zipOs;
}