Example usage for java.util.zip Deflater deflate

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

Introduction

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

Prototype

public int deflate(ByteBuffer output) 

Source Link

Document

Compresses the input data and fills specified buffer with compressed data.

Usage

From source file:Main.java

/**
 * DEFLATEs the specified input data./*from  w ww .j  a v  a  2 s  .  c  o m*/
 * 
 * @param data the input data
 * @param dictionary the dictionary, or null if none
 * @return the compressed data
 */
public static byte[] deflate(byte[] data, byte[] dictionary) {
    Deflater deflater = new Deflater(8, true);
    if (dictionary != null) {
        deflater.setDictionary(dictionary);
    }
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    while (!deflater.finished()) {
        int n = deflater.deflate(buffer);
        byteArrayOutputStream.write(buffer, 0, n);
    }
    byte[] result = byteArrayOutputStream.toByteArray();
    return result;
}

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 a  v a2 s . c o  m

    // 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:fr.eo.util.dumper.Dumper.java

private static String getCompressedString(String value) {

    byte[] output = new byte[8096];

    try {//  w  w  w .  j  ava 2 s .com
        byte[] input = value.getBytes("UTF-8");
        Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
        compresser.setInput(input);
        compresser.finish();
        int compressedDataLength = compresser.deflate(output);
        return "X'" + Hex.encodeHexString(Arrays.copyOf(output, compressedDataLength)) + "'";
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.ojbc.util.helper.ZipUtils.java

public static byte[] zip(byte[] originalData) {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    compressor.setInput(originalData);// w  w  w .j  a  va  2  s .  c om
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(originalData.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) {
        log.error("Failed to zip data " + originalData, e);
    }
    byte[] compressedData = bos.toByteArray();

    log.debug("Orignal data:" + originalData.length + " bytes");
    log.debug("Compressed data:" + compressedData.length + " bytes");
    return compressedData;
}

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 ww  . j  a  va 2s .  c om*/

    // 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();
}

From source file:org.atricore.idbus.capabilities.sso.main.binding.SamlR2HttpRedirectBinding.java

public static String deflateForRedirect(String redirStr, boolean encode) {

    int n = redirStr.length();
    byte[] redirIs = null;
    try {// w  w w  .j a  va 2s  .  c om
        redirIs = redirStr.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    byte[] deflated = new byte[n];

    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(redirIs);
    deflater.finish();
    int len = deflater.deflate(deflated);
    deflater.end();

    byte[] exact = new byte[len];

    System.arraycopy(deflated, 0, exact, 0, len);

    if (encode) {
        byte[] base64Str = new Base64().encode(exact);
        return new String(base64Str);
    }

    return new String(exact);
}

From source file:com.asual.lesscss.ResourcePackage.java

private static byte[] deflate(byte[] input) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(input);// w  w w  .j a  va2 s  .co  m
    deflater.finish();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        baos.write(buf, 0, count);
    }
    baos.close();
    return baos.toByteArray();
}

From source file:com.igormaznitsa.jcp.expression.functions.FunctionBINFILE.java

@Nonnull
private static byte[] deflate(@Nonnull final byte[] data) throws IOException {
    final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);/*www . j av a  2  s. com*/

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    final byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        final int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    final byte[] output = outputStream.toByteArray();

    deflater.end();

    return output;
}

From source file:radixcore.network.ByteBufIO.java

/**
 * Compresses the data in a byte array./*from  w w  w .ja va 2s.  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:ZipUtil.java

/**
 * Deflates the file and returns the deflated file.
 *//*from  www . j a va2  s.c  om*/
public static byte[] zipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;
    Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
    oDeflate.setInput(file);
    oDeflate.finish();
    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (!oDeflate.finished()) {
            byte[] byRead = new byte[ZIP_BUFFER_SIZE];
            int iBytesRead = oDeflate.deflate(byRead);
            if (iBytesRead == byRead.length) {
                oZipStream.write(byRead);
            } else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        oDeflate.end();
        byReturn = oZipStream.toByteArray();
    } finally {
        oZipStream.close();
    }
    return byReturn;
}