Android examples for java.util.zip:Zip
unGzip Byte Array To String
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PushbackInputStream; import java.util.zip.GZIPInputStream; public class Main { public static String unGzipBytesToString(InputStream in) { try {//from w ww.java 2s. c om PushbackInputStream pis = new PushbackInputStream(in, 2); byte[] signature = new byte[2]; pis.read(signature); pis.unread(signature); int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00)); if (head != GZIPInputStream.GZIP_MAGIC) { return new String(toByteArray(pis), "UTF-8").trim(); } GZIPInputStream gzip = new GZIPInputStream(pis); byte[] readBuf = new byte[8 * 1024]; ByteArrayOutputStream outputByte = new ByteArrayOutputStream(); int readCount = 0; do { readCount = gzip.read(readBuf); if (readCount > 0) { outputByte.write(readBuf, 0, readCount); } } while (readCount > 0); if (outputByte.size() > 0) { return new String(outputByte.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] toByteArray(InputStream input) throws Exception { if (input == null) { return null; } ByteArrayOutputStream output = null; byte[] result = null; try { output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 100]; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } result = output.toByteArray(); } finally { closeQuietly(input); closeQuietly(output); } return result; } public static void closeQuietly(InputStream is) { try { if (is != null) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } public static void closeQuietly(OutputStream os) { try { if (os != null) { os.close(); } } catch (Exception e) { e.printStackTrace(); } } }