Example usage for java.util.zip Deflater BEST_COMPRESSION

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

Introduction

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

Prototype

int BEST_COMPRESSION

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

Click Source Link

Document

Compression level for best compression.

Usage

From source file:com.izforge.izpack.compiler.container.provider.JarOutputStreamProvider.java

public JarOutputStream provide(CompilerData compilerData) {
    File file = new File(compilerData.getOutput());
    JarOutputStream jarOutputStream = null;
    FileOutputStream fileOutputStream = null;
    FileUtils.deleteQuietly(file);//from  www  . j  ava 2 s. com
    try {
        if (compilerData.isMkdirs()) {
            FileUtils.forceMkdirParent(file);
        }
        fileOutputStream = new FileOutputStream(file);
        jarOutputStream = new JarOutputStream(fileOutputStream);
        int level = compilerData.getComprLevel();
        if (level >= 0 && level < 10) {
            jarOutputStream.setLevel(level);
        } else {
            jarOutputStream.setLevel(Deflater.BEST_COMPRESSION);
        }
    } catch (IOException e) {
        IOUtils.closeQuietly(fileOutputStream);
    }

    return jarOutputStream;
}

From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java

public static void makeBackup() {
    File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH);
    if (!f.mkdirs() && !f.exists()) {
        _log.warn("Could not create folder " + f.getAbsolutePath());
        return;//from w ww  . j ava2s .  c o m
    }

    _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`...");

    Process run = null;
    try {
        run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password="
                + Config.DATABASE_PASSWORD
                + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers "
                + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH));
    } catch (Exception e) {
    } finally {
        if (run == null) {
            _log.warn("Could not execute mysqldump!");
            return;
        }
    }

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date time = new Date();

        File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql"));
        if (!bf.createNewFile())
            throw new IOException("Cannot create backup file: " + bf.getCanonicalPath());
        InputStream input = run.getInputStream();
        OutputStream out = new FileOutputStream(bf);
        if (Config.DATABASE_BACKUP_COMPRESSION) {
            ZipOutputStream dflt = new ZipOutputStream(out);
            dflt.setMethod(ZipOutputStream.DEFLATED);
            dflt.setLevel(Deflater.BEST_COMPRESSION);
            dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: "
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time));
            dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql"));
            out = dflt;
        }

        byte[] buf = new byte[4096];
        int written = 0;
        for (int read; (read = input.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
        input.close();
        out.close();

        if (written == 0) {
            bf.delete();
            BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream()));
            String line;
            while ((line = br.readLine()) != null)
                _log.warn("DatabaseBackupManager: " + line);
            br.close();
        } else
            _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME
                    + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000
                    + " s.");

        run.waitFor();
    } catch (Exception e) {
        _log.warn("DatabaseBackupManager: Could not make backup: ", e);
    }
}

From source file:org.getspout.spout.packet.PacketCacheFile.java

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(fileData);/*from w  w  w. j  av a2  s  .  c  o  m*/
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int bytesCompressed = deflater.deflate(buffer);
            bos.write(buffer, 0, bytesCompressed);
        }
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileData = bos.toByteArray();
        compressed = true;
    }
}

From source file:fr.eoit.util.dumper.DumperTest.java

@Test
public void testCompression() {

    byte[] strBytes = COPYRIGHTS.getBytes();

    byte[] output = new byte[8096];
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    compresser.setInput(strBytes);//from   w  ww. jav a 2s .com
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();

    String inputString = new String(Hex.encodeHex(strBytes));
    String hexString = new String(Arrays.copyOf(output, compressedDataLength));

    int i = 0;
    i++;
}

From source file:de.scoopgmbh.copper.persistent.StandardJavaSerializer.java

public void setCompressorMaxSize(int compressorMaxSize) {
    this.compressorMaxSize = compressorMaxSize;
    compressorTL = new ThreadLocal<Compressor>() {
        @Override//from ww w.j  a v  a  2  s  .  c  o m
        protected Compressor initialValue() {
            return new Compressor(Deflater.BEST_COMPRESSION, StandardJavaSerializer.this.compressorMaxSize);
        }
    };
}

From source file:spartanfinal.ProcessFiles.java

public byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);//ww w  . ja v a 2s. c om
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

public void setCompressorMaxSize(int compressorMaxSize) {
    this.compressorMaxSize = compressorMaxSize;
    compressorTL = new ThreadLocal<Compressor>() {
        @Override/*  w  ww.j a  v  a 2s .com*/
        protected Compressor initialValue() {
            return new Compressor(Deflater.BEST_COMPRESSION, TestJavaSerializer.this.compressorMaxSize);
        }
    };
}

From source file:radixcore.network.ByteBufIO.java

/**
 * Compresses the data in a byte array./*w  w  w .  j  av  a  2 s. c  o m*/
 * 
 * @param input The byte array to be compressed.
 * @return The byte array in its compressed form.
 */
public static byte[] compress(byte[] input) {
    try {
        final Deflater deflater = new Deflater();
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.setInput(input);

        final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
        deflater.finish();

        final byte[] buffer = new byte[1024];

        while (!deflater.finished()) {
            final int count = deflater.deflate(buffer);
            byteOutput.write(buffer, 0, count);
        }

        deflater.end();
        byteOutput.close();
        return byteOutput.toByteArray();
    }

    catch (final IOException e) {
        RadixExcept.logFatalCatch(e, "Error compressing byte array.");
        return null;
    }
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
    ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
    for (SecurityKeyData dr : devices) {
        KeyHolder kh = new KeyHolder();
        kh.setCounter(dr.getCounter());//w w  w . j  a  v  a 2s. c om
        kh.setEnrollmentTime(dr.getEnrollmentTime());
        kh.setKeyHandle(dr.getKeyHandle());
        kh.setPublicKey(dr.getPublicKey());
        kh.setTransports(dr.getTransports());
        keys.add(kh);
    }

    String json = gson.toJson(keys);
    EncryptedMessage msg = new EncryptedMessage();

    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
    if (key == null) {
        throw new Exception("Queue message encryption key not found");
    }

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
    msg.setIv(cipher.getIV());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DeflaterOutputStream compressor = new DeflaterOutputStream(baos,
            new Deflater(Deflater.BEST_COMPRESSION, true));

    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();

    String b64 = new String(Base64.encodeBase64(baos.toByteArray()));

    return b64;

}

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {/*from w  w  w. j a v  a 2s  .  co  m*/
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}