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: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. co m
    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

/**
 * Compress the byte array passed/*from w  w w . ja  v a 2 s  .  co m*/
 * <p>
 * @param input byte array
 * @param bufferLength buffer length
 * @return compressed byte array
 * @throws IOException thrown if we can't close the output stream
 */
public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException {
    // 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);
    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[bufferLength];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();

    // Get the compressed data
    return bos.toByteArray();

}

From source file:com.qatickets.common.ZIPHelper.java

public static byte[] compress(byte[] data) {

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

    // Give the compressor the data to compress
    compressor.setInput(data);/*w w  w  .j a  va2  s.  co  m*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

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

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

    compressor.end();

    return compressedData;
}

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

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(data);/*from w w w  . j  ava 2 s .co m*/
        deflater.setLevel(Deflater.BEST_COMPRESSION);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.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();
        }
        data = bos.toByteArray();
        compressed = true;
    }
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode lz byte [ ].//ww w  .  j  a  va  2  s .  c  o  m
 *
 * @param bytes      the bytes
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] encodeLZ(byte[] bytes, String dictionary) {
    byte[] output = new byte[(int) (bytes.length * 1.05 + 32)];
    Deflater compresser = new Deflater();
    try {
        compresser.setInput(bytes);
        if (null != dictionary && !dictionary.isEmpty()) {
            byte[] bytes2 = dictionary.getBytes("UTF-8");
            compresser.setDictionary(bytes2);
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();
    return Arrays.copyOf(output, compressedDataLength);
}

From source file:com.moesol.keys.EncodeUidsTest.java

public void testCompress() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; i++) {
        UUID id = UUID.randomUUID();
        sb.append(id);/*from ww w  .  j  a va2s . com*/
        sb.append(',');
    }
    String result = sb.toString();
    //      System.out.println("val=" + result);

    Deflater deflate = new Deflater();
    try {
        byte[] compressed = new byte[512000];
        deflate.setInput(result.getBytes());
        deflate.finish();
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
        deflate.deflate(compressed);
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
    } finally {
        deflate.end();
    }
}

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

public void compress() {
    if (!compressed) {
        Deflater deflater = new Deflater();
        deflater.setInput(fileData);//  w w w .j  a  va 2  s. co 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:spartanfinal.ProcessFiles.java

public byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);//from  w ww. j a  v a 2  s.  c o m
    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:radixcore.network.ByteBufIO.java

/**
 * Compresses the data in a byte array.// w ww  . j ava2 s. c om
 * 
 * @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;
    }
}