Example usage for java.util.zip Inflater inflate

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

Introduction

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

Prototype

public int inflate(ByteBuffer output) throws DataFormatException 

Source Link

Document

Uncompresses bytes into specified buffer.

Usage

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//  w ww . ja  v a  2 s  .  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();

    System.out.println("Original: " + data.length);
    System.out.println("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);// ww  w .j a v a 2 s.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();
    inflater.end();

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

From source file:Main.java

/**
 * Decompress the zlib-compressed bytes and return an array of decompressed
 * bytes//from  w  w  w.  ja  v  a 2  s  .  co m
 * 
 */
public static byte[] decompress(byte compressedBytes[]) throws DataFormatException {

    Inflater decompresser = new Inflater();

    decompresser.setInput(compressedBytes);

    byte[] resultBuffer = new byte[compressedBytes.length * 2];
    byte[] resultTotal = new byte[0];

    int resultLength = decompresser.inflate(resultBuffer);

    while (resultLength > 0) {
        byte previousResult[] = resultTotal;
        resultTotal = new byte[resultTotal.length + resultLength];
        System.arraycopy(previousResult, 0, resultTotal, 0, previousResult.length);
        System.arraycopy(resultBuffer, 0, resultTotal, previousResult.length, resultLength);
        resultLength = decompresser.inflate(resultBuffer);
    }

    decompresser.end();

    return resultTotal;
}

From source file:Main.java

public static byte[] decompress(byte[] input, boolean GZIPFormat) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater(GZIPFormat);
    decompressor.setInput(input);// ww  w. j a v a2s .c o  m
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!decompressor.finished()) {
        readCount = decompressor.inflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }
    decompressor.end();
    return bao.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);//from  w  w w. j a v  a 2 s  .  c o  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!decompresser.finished()) {
        int count = decompresser.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    decompresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public final static byte[] decompress(byte[] input) throws IOException {
    if (input == null || input.length == 0) {
        return input;
    }//from w w  w  .java  2 s  .  co  m
    Inflater inflator = new Inflater();
    inflator.setInput(input);
    ByteArrayOutputStream bin = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[BUFFER_SIZE];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bin.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new IOException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new IOException(t);
    } finally {
        inflator.end();
    }
    return bin.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from w ww  .  j a  v a  2s. c om
    inflater.finished();

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

From source file:Main.java

public static byte[] zipDecompress(byte[] input) throws IOException {
    Inflater inflator = new Inflater();
    inflator.setInput(input);//from  ww  w .jav a  2 s . c o m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bos.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new RuntimeException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new RuntimeException(t);
    } finally {
        inflator.end();
    }
    return bos.toByteArray();
}

From source file:Main.java

public static void decompress(byte[] data, int off, int len, OutputStream out) {
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  w w w.  ja  v  a2s . c o m*/
    decompresser.setInput(data, off, len);
    byte[] buf = new byte[1024];

    try {
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            out.write(buf, 0, i);
            out.flush();
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        decompresser.end();
    }
}

From source file:Main.java

public static byte[] decompressInZlib(byte[] compressData, int offset, int length) throws Exception {

    Inflater decompresser = new Inflater();
    decompresser.setInput(compressData, 0, compressData.length);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    int count;//from  w  w w. j  a v a2s. co  m
    byte[] buf = new byte[1024];
    while (!decompresser.finished()) {
        count = decompresser.inflate(buf);
        bos.write(buf, 0, count);
    }

    byte[] originalData = bos.toByteArray();
    decompresser.end();

    return originalData;
}