Try this -
Code:
/**
* Uncompress a previously compressed string;
* this method is the inverse of the compress method.
* @param byte array containing compressed data
* @return uncompressed string
* @throws IOException if the inflation fails
*/
public static final byte[] uncompress(final byte[] compressed) throws IOException {
// String uncompressed = "";
byte[] result = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
GZIPInputStream zis = new GZIPInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int numBytesRead = 0;
byte[] tempBytes = new byte[DEFAULT_BUFFER_SIZE];
while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
baos.write(tempBytes, 0, numBytesRead);
}
result = (baos.toByteArray());
} catch (Exception e) {
System.out.println("Error while unzip " + e.toString());
}
return result;
}
/**
* Compress a string
* @param uncompressed string
* @return byte array containing compressed data
* @throws IOException if the deflation fails
*/
public static final byte[] compress(final byte[] uncompressed) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream zos = new GZIPOutputStream(baos);
byte[] uncompressedBytes = uncompressed;
zos.write(uncompressedBytes, 0, uncompressedBytes.length);
zos.close();
return baos.toByteArray();
}