Daniel Alome
Daniel Alome

Reputation: 99

is it possible to convert a Gzip byte array to String?

i am able to compress a base64 string areng the GZIP java compression method, so it is possible to get the compressed value in a st ring?

Upvotes: 1

Views: 2703

Answers (1)

makif
makif

Reputation: 300

Yes, you can decompress gzip to string with this method.

public static String decompress(byte[] byteArray) throws Exception {
    if (byteArray == null ) {
        return null;
    }
    
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(byteArray));
    BufferedReader bfr = new BufferedReader(new InputStreamReader(gzis, "UTF-8"));
    String outputString = "";
    String line;
    while ((line=bfr.readLine())!=null) {
      outputString += line;
    }
    return outputString;
 }

Upvotes: 1

Related Questions