Java Uncompress Byte Array unzip(byte[] bytes)

Here you can find the source of unzip(byte[] bytes)

Description

unzip

License

Open Source License

Declaration

public static byte[] unzip(byte[] bytes) 

Method Source Code

//package com.java2s;

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.LinkedList;
import java.util.List;

import java.util.zip.ZipInputStream;

public class Main {
    public static byte[] unzip(byte[] bytes) {
        try {/*from   w  w  w .j a va2  s  . c  om*/
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ZipInputStream zis = new ZipInputStream(bis);
            zis.getNextEntry();
            return toByteArray(zis, -1);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    public static byte[] toByteArray(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i += 2) {
            String hex = hexString.substring(i, i + 2);
            bytes[i / 2] = (byte) Integer.parseInt(hex, 16);
        }
        return bytes;
    }

    /**
     * Converts (a portion) of the given stream to a byte array.
     * 
     * @param inputStream
     *            the stream from which to read bytes
     * @param size
     *            the number of bytes to be read, or -1 if the whole of the
     *            (remaining) bytes should be read
     * @return a byte array of length <tt>size</tt> containing bytes read from
     *         the given stream, starting at the current position
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream inputStream, final int size) throws IOException {
        int totalSize = 0;
        List<byte[]> bufferList = null;
        byte[] result = null;
        if (size == -1) {
            bufferList = new LinkedList<byte[]>();
        } else {
            result = new byte[size];
        }
        int avail = inputStream.available();
        while (avail > 0) {
            byte[] buffer = new byte[1024];
            int len;
            if (size == -1) {
                len = 1024;
            } else {
                len = Math.min(1024, size - totalSize);
            }
            int r = inputStream.read(buffer, 0, len);
            if (r == -1) {
                break;
            }
            if (size == -1) {
                if (r < 1024) {
                    byte[] smallerBuffer = new byte[r];
                    System.arraycopy(buffer, 0, smallerBuffer, 0, r);
                    bufferList.add(smallerBuffer);
                } else {
                    bufferList.add(buffer);
                }
            } else {
                System.arraycopy(buffer, 0, result, totalSize, r);
            }
            totalSize += r;
            if (size != -1 && totalSize == size) {
                break;
            }
        }
        if (size == -1) {
            result = new byte[totalSize];
            int offset = 0;
            for (byte[] buffer : bufferList) {
                System.arraycopy(buffer, 0, result, offset, buffer.length);
                offset += buffer.length;
            }
        }
        return result;
    }

    public static String substring(String string, int pos, int length) {
        if (string.length() - pos <= length) {
            return string.substring(pos);
        }
        return string.substring(pos, pos + length);
    }
}

Related

  1. uncompress(byte[] b)
  2. uncompress(byte[] input)
  3. unzip(byte zipData[])
  4. unzip(byte[] blob)
  5. unzip(byte[] bytes)
  6. unzip(byte[] bytes)
  7. unzip(byte[] bytes, String encoding)
  8. unzip(byte[] compressedByte)
  9. unzip(byte[] compressedData)