Example usage for java.util.zip Deflater Deflater

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

Introduction

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

Prototype

public Deflater() 

Source Link

Document

Creates a new compressor with the default compression level.

Usage

From source file:Main.java

public static String compressString(String str, String encoding) {
    String compressedString = "";

    try {/*  ww  w  .j  a va2s. c o  m*/
        byte[] input = str.getBytes(encoding);
        Deflater d = new Deflater();
        d.setLevel(Deflater.BEST_COMPRESSION);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
        dout.write(input);
        dout.close();
        compressedString = new String(Hex.encodeHex(out.toByteArray()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return compressedString;
}

From source file:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/* w  w  w.  j  a v  a2 s  . com*/
    try {
        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);
        }
        outputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        deflater.end();
    }

}

From source file:Main.java

public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();/*w  ww.  ja  va 2 s.c  om*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    int count;
    byte[] buf = new byte[1024];
    while (!compresser.finished()) {
        count = compresser.deflate(buf);
        bos.write(buf, 0, count);
    }
    compresser.end();

    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:Main.java

public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception {

    byte[] input = bytes;

    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*from  w ww  .  j av  a  2 s.  c  om*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[32];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();

    return new String(Base64.encode(compressedData), "UTF-8");
}

From source file:Main.java

public static byte[] compress(byte[] data, int level) throws IOException {
    if (data == null || data.length == 0) {
        return data;
    }//  w w w . j av  a 2 s  . com
    ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
    Deflater deflater = new Deflater();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    byte[] buf = new byte[BUFFER_SIZE];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bout.write(buf, 0, count);
    }
    deflater.end();
    bout.close();
    return bout.toByteArray();
}

From source file:com.barrybecker4.common.util.Base64Codec.java

/**
 * take a String and compress it./*from   ww  w .j ava2  s .  c om*/
 * See @decompress for reversing the compression.
 * @param data a string to compress.
 * @return compressed string representation.
 */
public static synchronized String compress(final String data) {

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
    Deflater deflater = new Deflater();
    DeflaterOutputStream oStream = new DeflaterOutputStream(byteOut, deflater);

    try {
        oStream.write(data.getBytes(CONVERTER_UTF8));
        oStream.flush();
        oStream.close();
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e);
    } catch (IOException e) {
        throw new IllegalStateException("io error :" + e.getMessage(), e);
    }

    return new String(Base64.encodeBase64(byteOut.toByteArray()));
}

From source file:Main.java

/**
 * Creates a SyncFlush Deflater for use on pre-KitKat Android
 *
 * @return The modified Deflater, or null if the creation failed
 *//* ww w  .j a va  2s.co  m*/
@Nullable
private static Deflater createSyncFlushDeflater() {
    Deflater def = new Deflater();
    try {
        Field f = def.getClass().getDeclaredField("flushParm");
        f.setAccessible(true);
        f.setInt(def, 2); // Z_SYNC_FLUSH
    } catch (Exception e) {
        return null;
    }
    return def;
}

From source file:Main.java

public static byte[] zipCompress(byte[] input, int level) {
    Deflater compressor = new Deflater();
    compressor.setLevel(level);//from   w  ww. j a va2  s . c  o  m
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    return bos.toByteArray();
}

From source file:org.odk.collect.android.utilities.CompressionUtils.java

public static String compress(String data) throws IOException {
    if (data == null || data.length() == 0) {
        return data;
    }// w w  w.  j av a2 s . c  om

    // Encode string into bytes
    byte[] input = data.getBytes("UTF-8");

    Deflater deflater = new Deflater();
    deflater.setInput(input);

    // Compress the bytes
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length());
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    // Encode to base64
    String base64String = Base64.encodeBase64String(output);
    Timber.i("Original : %d", data.length());
    Timber.i("Compressed : %d", base64String.length());
    Timber.i("Compression ratio : %2f", ((data.length() * 1.0) / base64String.length()) * 100);
    return base64String;
}

From source file:Main.java

public static byte[] Compress(String text) throws Exception {
    Deflater compressor = new Deflater();
    byte[] bytes = text.getBytes("UTF-16LE");
    compressor.setInput(bytes);/*from w  w w  .  j  ava 2  s.c  o m*/

    // Create an expandable byte array to hold the compressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    compressor.finish();

    byte[] buffer = new byte[1024];
    try {
        while (!compressor.finished()) {
            int count = compressor.deflate(buffer);
            bos.write(buffer, 0, count);
        }
    } finally {
        compressor.finish();
    }

    bos.close();
    return bos.toByteArray();
}