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

private static byte[] expand(byte[] bytes, int skip) {
    byte[] newBytes = new byte[bytes.length - skip];
    Inflater inflater = new Inflater();

    inflater.setInput(bytes, skip, newBytes.length);
    try {/* w w  w. j  a  v  a2s .  c  om*/
        int outCount = inflater.inflate(newBytes);
        System.arraycopy(newBytes, 0, bytes, skip, outCount);
        Arrays.fill(bytes, skip + outCount, bytes.length, (byte) 0);
        return bytes;
    } catch (DataFormatException e) {
    }

    return null;
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from   w  w  w . ja  va  2  s  .  com
    inflater.finished();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    try {
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    } catch (DataFormatException ex) {
        throw new IOException(ex);
    }

    byte[] output = outputStream.toByteArray();
    inflater.end();
    return output;
}

From source file:Main.java

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

    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

public static void decompress(byte[] data, int off, int len, OutputStream out) {
    Inflater decompresser = new Inflater();
    decompresser.reset();// w  ww  .  j  a va2s.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 ww  . ja  va 2s .c  o 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;
}

From source file:Main.java

public final static byte[] decompress(byte[] input) throws IOException {
    if (input == null || input.length == 0) {
        return input;
    }/*w  ww.  ja va  2  s.  c  o  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[] zlibDecompress(byte[] data, int offset, int length) {
    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  w  w  w  .  ja  v a 2 s . c  o  m*/
    try {
        decompresser.setInput(data, offset, length);
    } catch (Exception e) {
        return null;
    }

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

    decompresser.end();
    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 ww  w .  j  av a2s  . c o  m
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

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

    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);//  www. j  av  a 2  s.  c  om
    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();
}