uncompress String with Gzip - Android File Input Output

Android examples for File Input Output:Zip File

Description

uncompress String with Gzip

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.util.zip.GZIPInputStream;

import android.util.Base64;

public class Main {
    public static String uncompress(String str) {
        if (str == null || str.length() == 0) {
            return "";
        }//from  w  ww  .  j a  v  a2s. c om
        try {
            byte[] t = Base64.decode(str, 0);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(t);
            GZIPInputStream gunzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = gunzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            gunzip.close();
            in.close();
            out.close();
            return out.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials