Read GZIP and return String - Android File Input Output

Android examples for File Input Output:Zip File

Description

Read GZIP and return String

Demo Code


//package com.java2s;

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

public class Main {
    private static final int BUFFER_SIZE = 1024;

    public static String decryptGZIP(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }//from   w  w w  .  j  av a2  s. c om

        try {

            byte[] decode = str.getBytes("UTF-8");

            ByteArrayInputStream bais = new ByteArrayInputStream(decode);
            GZIPInputStream gzip = new GZIPInputStream(bais);

            byte[] buf = new byte[BUFFER_SIZE];
            int len = 0;

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ((len = gzip.read(buf, 0, BUFFER_SIZE)) != -1) {
                baos.write(buf, 0, len);
            }
            gzip.close();
            baos.flush();

            decode = baos.toByteArray();

            baos.close();

            return new String(decode, "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials