uncompress String and InputStream to String - Android java.io

Android examples for java.io:InputStream

Description

uncompress String and InputStream to String

Demo Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

public class Main {

  public static String uncompress(String str) throws IOException {
    if (str == null || str.length() == 0) {
      return str;
    }/*from  w ww  . jav  a 2  s.  c  om*/
    ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
    return uncompress(in);
  }

  public static String uncompress(InputStream inputStream) throws IOException {
    if (inputStream == null) {
      return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPInputStream gunzip = new GZIPInputStream(inputStream);
    byte[] buffer = new byte[256];
    int n;
    while ((n = gunzip.read(buffer)) >= 0) {
      out.write(buffer, 0, n);
    }
    return out.toString();
  }

} 

Related Tutorials