Java Gunzip File gunzip(String compressedStr)

Here you can find the source of gunzip(String compressedStr)

Description

gunzip

License

Apache License

Declaration

public static String gunzip(String compressedStr) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }//from  ww  w . ja v  a  2s.  co m

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }
}

Related

  1. gunzip(File gzippedFile)
  2. gunzip(File gzippedFile, File destinationFile)
  3. gUnzip(File srcFile, File destFile)
  4. gunzip(InputStream packedData, OutputStream unpackedData)
  5. gunzip(String inFile, String outFile)
  6. gunzipFile(File baseDir, File gzFile)
  7. gunzipFile(File file_input, File dir_output)
  8. gunzipFile(File inputGZippedFile, File outputFile)