Example usage for java.util.zip Deflater DEFAULT_COMPRESSION

List of usage examples for java.util.zip Deflater DEFAULT_COMPRESSION

Introduction

In this page you can find the example usage for java.util.zip Deflater DEFAULT_COMPRESSION.

Prototype

int DEFAULT_COMPRESSION

To view the source code for java.util.zip Deflater DEFAULT_COMPRESSION.

Click Source Link

Document

Default compression level.

Usage

From source file:Main.java

public static byte[] zipCompress(byte[] input) {
    return zipCompress(input, Deflater.DEFAULT_COMPRESSION);
}

From source file:com.thoughtworks.go.server.cache.ZipArtifactCache.java

@Override
void createCachedFile(ArtifactFolder artifactFolder) throws IOException {
    File originalFolder = artifactFolder.getRootFolder();
    File cachedZip = cachedFile(artifactFolder);
    File cachedTempZip = zipToTempFile(cachedZip);
    cachedTempZip.getParentFile().mkdirs();
    try {/*from w  ww  .  j av a  2  s.co  m*/
        zipUtil.zip(originalFolder, cachedTempZip, Deflater.DEFAULT_COMPRESSION);
    } catch (IOException e) {
        cachedTempZip.delete();
        throw e;
    }
    FileUtils.moveFile(cachedTempZip, cachedZip);
}

From source file:it.geosolutions.mariss.wps.ppio.OutputResourcesPPIO.java

@Override
public void encode(Object value, OutputStream os) throws Exception {

    ZipOutputStream zos = null;/*from  w w w  .  j  a v  a2  s .c om*/
    try {
        OutputResource or = (OutputResource) value;

        zos = new ZipOutputStream(os);
        zos.setMethod(ZipOutputStream.DEFLATED);
        zos.setLevel(Deflater.DEFAULT_COMPRESSION);

        Iterator<File> iter = or.getDeletableResourcesIterator();
        while (iter.hasNext()) {

            File tmp = iter.next();
            if (!tmp.exists() || !tmp.canRead() || !tmp.canWrite()) {
                LOGGER.warning("Skip Deletable file '" + tmp.getName() + "' some problems occurred...");
                continue;
            }

            addToZip(tmp, zos);

            if (!tmp.delete()) {
                LOGGER.warning("File '" + tmp.getName() + "' cannot be deleted...");
            }
        }
        iter = null;

        Iterator<File> iter2 = or.getUndeletableResourcesIterator();
        while (iter2.hasNext()) {

            File tmp = iter2.next();
            if (!tmp.exists() || !tmp.canRead()) {
                LOGGER.warning("Skip Undeletable file '" + tmp.getName() + "' some problems occurred...");
                continue;
            }

            addToZip(tmp, zos);

        }
    } finally {
        try {
            zos.close();
        } catch (IOException e) {
            LOGGER.severe(e.getMessage());
        }
    }
}

From source file:com.thoughtworks.go.server.database.MigrateHsqldbToH2.java

private void backupNewTemplateDb(File dbDirectory, File newDb) throws IOException {
    File backup = new File(dbDirectory, "h2db-template-backup-" + dateString() + ".zip");
    new ZipUtil().zip(newDb, backup, Deflater.DEFAULT_COMPRESSION);
    deleteDirectory(newDb);/*from w  ww.j  a v  a 2  s  . c om*/
    if (newDb.exists()) {
        bomb("Database " + newDb + " could not be deleted.");
    }
}

From source file:com.thoughtworks.go.server.database.MigrateHsqldbToH2.java

private void backupOldDb(File dbDirectory, File oldHsql) throws IOException {
    File backupFile = new File(dbDirectory, "hsqldb-upgrade-backup-" + dateString() + ".zip");
    if (backupFile.exists()) {
        bomb(BACKUP_ALREADY_EXISTS);/*from  ww w .  j  a  v a  2s  .  c  om*/
    }
    new ZipUtil().zip(oldHsql, backupFile, Deflater.DEFAULT_COMPRESSION);
}

From source file:juicebox.tools.utils.original.Preprocessor.java

public Preprocessor(File outputFile, String genomeId, List<Chromosome> chromosomes) {
    this.genomeId = genomeId;
    this.outputFile = outputFile;
    this.matrixPositions = new LinkedHashMap<String, IndexEntry>();

    this.chromosomes = chromosomes;
    chromosomeIndexes = new Hashtable<String, Integer>();
    for (int i = 0; i < chromosomes.size(); i++) {
        chromosomeIndexes.put(chromosomes.get(i).getName(), i);
    }/*from   w  w w .  j av  a2  s . c  o  m*/

    compressor = new Deflater();
    compressor.setLevel(Deflater.DEFAULT_COMPRESSION);

    this.tmpDir = null;

}

From source file:org.resthub.rpc.AMQPProxy.java

/**
 * Create the request message body//from   w  w  w  . j a v a2  s . c o m
 *
 * @param method
 * @param args
 * @return
 * @throws IOException
 */
private byte[] createRequestBody(Method method, Object[] args) throws IOException {
    ByteArrayOutputStream payload = new ByteArrayOutputStream(256);
    OutputStream os;
    if (_factory.isCompressed()) {
        Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
        os = new DeflaterOutputStream(payload, deflater);
    } else {
        os = payload;
    }

    _factory.getSerializationHandler().writeMethodCall(method, args, os);

    return payload.toByteArray();
}

From source file:com.edgenius.core.util.ZipFileUtil.java

/**
 * Creates a ZIP file and places it in the current working directory. The zip file is compressed
 * at the default compression level of the Deflater.
 * /*  w  ww.j a va  2s  . c  om*/
 * @param listToZip. Key is file or directory, value is parent directory which will remove from given file/directory because 
 * compression only save relative directory.  For example, c:\geniuswiki\data\repository\somefile, if value is c:\geniuswiki, then only 
 * \data\repository\somefile will be saved.  It is very important, the value must be canonical path, ie, c:\my document\geniuswiki, 
 * CANNOT like this "c:\my doc~1\" 
 * 
 * 
 */
public static void createZipFile(String zipFileName, Map<File, String> listToZip, boolean withEmptyDir)
        throws ZipFileUtilException {
    ZipOutputStream zop = null;
    try {
        zop = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
        zop.setMethod(ZipOutputStream.DEFLATED);
        zop.setLevel(Deflater.DEFAULT_COMPRESSION);

        for (Entry<File, String> entry : listToZip.entrySet()) {
            File file = entry.getKey();
            if (!file.exists()) {
                log.warn("Unable to find file " + file + " to zip");
                continue;
            }
            if (file.isDirectory()) {
                Collection<File> list = FileUtils.listFiles(file, null, true);
                for (File src : list) {
                    addEntry(zop, src, createRelativeDir(src.getCanonicalPath(), entry.getValue()));
                }
                if (withEmptyDir) {
                    final List<File> emptyDirs = new ArrayList<File>();
                    if (file.list().length == 0) {
                        emptyDirs.add(file);
                    } else {
                        //I just don't know how quickly to find out all empty sub directories recursively. so use below hack:
                        FileUtils.listFiles(file, FileFilterUtils.falseFileFilter(), new IOFileFilter() {
                            //JDK1.6 @Override
                            public boolean accept(File f) {
                                if (!f.isDirectory())
                                    return false;

                                int size = f.listFiles().length;
                                if (size == 0) {
                                    emptyDirs.add(f);
                                }
                                return true;
                            }

                            //JDK1.6 @Override
                            public boolean accept(File arg0, String arg1) {
                                return true;
                            }
                        });
                    }
                    for (File src : emptyDirs) {
                        addEntry(zop, null, createRelativeDir(src.getCanonicalPath(), entry.getValue()));
                    }
                }
            } else {
                addEntry(zop, file, createRelativeDir(file.getCanonicalPath(), entry.getValue()));
            }

        }
    } catch (IOException e1) {
        throw new ZipFileUtilException(
                "An error has occurred while trying to zip the files. Error message is: ", e1);
    } finally {
        try {
            if (zop != null)
                zop.close();
        } catch (Exception e) {
        }
    }

}

From source file:de.fu_berlin.inf.dpp.core.zip.FileZipper.java

private static void internalZipFiles(List<FileWrapper> files, File archive, boolean compress,
        boolean includeDirectories, long totalSize, ZipListener listener)
        throws IOException, OperationCanceledException {

    byte[] buffer = new byte[BUFFER_SIZE];

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(archive), BUFFER_SIZE);

    ZipOutputStream zipStream = new ZipOutputStream(outputStream);

    zipStream.setLevel(compress ? Deflater.DEFAULT_COMPRESSION : Deflater.NO_COMPRESSION);

    boolean cleanup = true;
    boolean isCanceled = false;

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from www  .  j  a va  2  s . com*/

    long totalRead = 0L;

    try {
        for (FileWrapper file : files) {
            String entryName = includeDirectories ? file.getPath() : file.getName();

            if (listener != null) {
                isCanceled = listener.update(file.getPath());
            }

            log.trace("compressing file: " + entryName);

            zipStream.putNextEntry(new ZipEntry(entryName));

            InputStream in = null;

            try {
                int read = 0;
                in = file.getInputStream();
                while (-1 != (read = in.read(buffer))) {

                    if (isCanceled) {
                        throw new OperationCanceledException(
                                "compressing of file '" + entryName + "' was canceled");
                    }

                    zipStream.write(buffer, 0, read);

                    totalRead += read;

                    if (listener != null) {
                        listener.update(totalRead, totalSize);
                    }

                }
            } finally {
                IOUtils.closeQuietly(in);
            }
            zipStream.closeEntry();
        }
        cleanup = false;
    } finally {
        IOUtils.closeQuietly(zipStream);
        if (cleanup && archive != null && archive.exists() && !archive.delete()) {
            log.warn("could not delete archive file: " + archive);
        }
    }

    stopWatch.stop();

    log.debug(String.format("created archive %s I/O: [%s]", archive.getAbsolutePath(),
            CoreUtils.throughput(archive.length(), stopWatch.getTime())));

}

From source file:name.npetrovski.jphar.DataEntry.java

private OutputStream getCompressorOutputStream(final OutputStream os, Compression.Type compression)
        throws IOException {
    switch (compression) {
    case ZLIB://from   w  ww  . j  a va  2 s. co m
        return new DeflaterOutputStream(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
    case BZIP:
        return new BZip2CompressorOutputStream(os);
    case NONE:
        return os;
    default:
        throw new IOException("Unsupported compression type.");
    }
}