Example usage for java.util.zip Inflater Inflater

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

Introduction

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

Prototype

public Inflater() 

Source Link

Document

Creates a new decompressor.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] compressedData = null;
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);//from  w  ww  .j  av  a  2s.c  o  m

    }
    bos.close();
    byte[] decompressedData = bos.toByteArray();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "this is a test".getBytes();

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

    compressor.setInput(input);//from  w  w  w .  j  a  v a  2s .  com
    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);
    }
    bos.close();

    byte[] compressedData = bos.toByteArray();
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    bos = new ByteArrayOutputStream(compressedData.length);

    buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] decompressedData = bos.toByteArray();
    System.out.println(new String(decompressedData));
}

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);/*  www .j  a va2s .  c  o  m*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:Main.java

public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];

    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  ww w .java2  s  .  c  om*/
    decompresser.setInput(data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(2 * data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int length = decompresser.inflate(buf);
            bos.write(buf, 0, length);
        }
        output = bos.toByteArray();
        bos.close();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    }

    decompresser.end();
    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] compressedBuffer) {
    Inflater inflater = new Inflater();
    inflater.setInput(compressedBuffer);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedBuffer.length);
    try {// w w  w  .  ja v  a2s.co  m
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;
    } catch (DataFormatException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            inflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

public static String uncompress(byte[] data) throws IOException, DataFormatException {

    Inflater inflater = new Inflater();
    inflater.setInput(data);/*  ww w  .  ja  v  a 2s.  c  o m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

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

    // Decode the bytes into a String
    return new String(output, 0, output.length, "UTF-8");
}

From source file:Main.java

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);/*from   ww w .j a va  2  s .com*/
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IOException(e.toString());
        }
    }
    bos.close();

    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();
    return decompressedData;
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*w  ww  . j a  v a 2 s . co  m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    /*
    LOG.debug("Original: " + data.length);
    LOG.debug("Compressed: " + output.length);
     */
    return output;
}

From source file:Main.java

private static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*from  ww w  . ja  va2 s  . com*/

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    inflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Decompressed: " + output.length + " bytes.");
    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {

    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();//from   www .j  av  a  2  s. co m
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    try {
        byte[] result = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(result);
            out.write(result, 0, i);
        }
        output = out.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        decompresser.end();
    }
    return output;
}