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[] uncompressBytesInflateDeflate(byte[] inBytes) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(inBytes);//from  w  w w. j  a  v  a 2 s . c o  m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    byte[] buffer = new byte[1024 * 8];
    while (!inflater.finished()) {
        int count;
        try {
            count = inflater.inflate(buffer);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:Main.java

/**
 * Decompress the zlib-compressed bytes and return an array of decompressed
 * bytes//from  ww w  . j av a 2 s  .c o 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[] zipDecompress(byte[] input) throws IOException {
    Inflater inflator = new Inflater();
    inflator.setInput(input);/*from   w  w  w  .ja v  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

/**
 * <p>Decompresses a the content of a file from {@code startPosition} of {@code compressedSize}
 * and return the bytes of it.</p>
 *
 * @param file           the file/*w ww.j a  v a 2 s  .co m*/
 * @param startPosition  the start position
 * @param compressedSize the compresse
 * @return the decompressed bytes
 * @throws IOException         if there was any io issues
 * @throws DataFormatException if there was an issue decompressing content
 */
public static byte[] decompress(File file, int startPosition, int compressedSize)
        throws IOException, DataFormatException {

    try (FileChannel fileChannel = FileChannel.open(file.toPath())) {
        ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, compressedSize);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Inflater inflater = new Inflater();
        byte[] compressedBytes = new byte[compressedSize];
        byte[] inflated = new byte[BUFSIZ];
        int read;

        buffer.get(compressedBytes);
        inflater.setInput(compressedBytes);

        // unzip contents
        while ((read = inflater.inflate(inflated)) != 0) {
            baos.write(inflated, 0, read);
        }

        inflater.end();

        return baos.toByteArray();
    }
}

From source file:Main.java

/**
 * Decompresses the given byte[] and returns the Object.
 * // w  ww . j  a  va2  s  .  c  om
 * @param <T> Type of expected Object
 * @param bytes compressed byte[]
 * @param bufferSize size of buffer to be used (in bytes)
 * 
 * @return decompressed Object
 * 
 * @throws IOException if failed to decompress
 * @throws ClassCastException if cannot cast to specified type
 */
@SuppressWarnings("unchecked")
/* Ignore Unchecked Cast Warning */
public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException {

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize);
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);

        // Decompress
        byte[] buf = new byte[bufferSize];
        while (!inflater.finished()) {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

        return (T) ois.readObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len, int srcLen) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();//from  ww  w.  j a  v  a 2 s  . c  om
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream o = new ByteArrayOutputStream(srcLen);
    try {
        o.reset();
        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

/**
 * @param data                  -- the data to decompress
 * @param uncompressedChunkSize -- an estimate of the uncompressed chunk size.  This need not be exact.
 * @return/*  w  ww  .j  a va  2  s  .c om*/
 */
public static byte[] decompress(byte[] data, int uncompressedChunkSize) {

    // mpd: new code
    int rem = data.length;

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedChunkSize);

    // Decompress the data
    byte[] outbuf = new byte[uncompressedChunkSize];

    Inflater decompressor = new Inflater();
    decompressor.setInput(data);
    while (rem > 0) {

        // If we are finished with the current chunk start a new one
        if (decompressor.finished()) {
            decompressor = new Inflater();
            int offset = data.length - rem;
            decompressor.setInput(data, offset, rem);
        }

        try {
            int count = decompressor.inflate(outbuf, 0, outbuf.length);
            rem = decompressor.getRemaining();
            bos.write(outbuf, 0, count);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    try {
        bos.close();
    } catch (IOException e) {
        // Ignore -- no resources open
    }

    // Return the decompressed data
    return bos.toByteArray();
}

From source file:com.barrybecker4.common.util.Base64Codec.java

/**
 * Take a String and decompress it.//from  ww w .  jav  a  2 s .c om
 * @param data the compressed string to decompress.
 * @return the decompressed string.
 */
public static synchronized String decompress(final String data) {

    // convert from string to bytes for decompressing
    byte[] compressedDat = Base64.decodeBase64(data.getBytes());

    final ByteArrayInputStream in = new ByteArrayInputStream(compressedDat);
    final Inflater inflater = new Inflater();
    final InflaterInputStream iStream = new InflaterInputStream(in, inflater);
    final char cBuffer[] = new char[4096];
    StringBuilder sBuf = new StringBuilder();
    try {
        InputStreamReader iReader = new InputStreamReader(iStream, CONVERTER_UTF8);
        while (true) {
            final int numRead = iReader.read(cBuffer);
            if (numRead == -1) {
                break;
            }
            sBuf.append(cBuffer, 0, numRead);
        }
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e);
    } catch (IOException e) {
        throw new IllegalStateException("io error :" + e.getMessage(), e);
    }

    return sBuf.toString();
}

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

public static byte[] unzip(byte[] data) {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from  w w  w.  j ava  2s  .  co  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    try {
        while (!inflater.finished()) {
            int count;
            count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    } catch (Exception e) {
        log.error("Failed to unzip data", e);
    }

    byte[] output = outputStream.toByteArray();
    log.debug("Original: " + data.length + " bytes");
    log.debug("Decompressed: " + output.length + " bytes");
    return output;
}

From source file:org.odk.collect.android.utilities.CompressionUtils.java

public static String decompress(String compressedString) throws IOException, DataFormatException {
    if (compressedString == null || compressedString.length() == 0) {
        return compressedString;
    }//from  w w  w.  j a  v  a 2 s  .  c  om

    // Decode from base64
    byte[] output = Base64.decodeBase64(compressedString);

    Inflater inflater = new Inflater();
    inflater.setInput(output);

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

    // Decode the bytes into a String
    String outputString = new String(result, "UTF-8");
    Timber.i("Compressed : %d", output.length);
    Timber.i("Decompressed : %d", result.length);
    return outputString;

}