Java Unzip File unCompressGzipFile(String path)

Here you can find the source of unCompressGzipFile(String path)

Description

un Compress Gzip File

License

Open Source License

Declaration

public static String unCompressGzipFile(String path) 

Method Source Code

//package com.java2s;
/**/*w w w  . j a va2s  . c om*/
 *Copyright (C) 2012-2013  Wikimedia Foundation
 *
 *This program is free software; you can redistribute it and/or
 *modify it under the terms of the GNU General Public License
 *as published by the Free Software Foundation; either version 2
 *of the License, or (at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program; if not, write to the Free Software
 *Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
 */

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;

public class Main {
    public static String unCompressGzipFile(String path) {
        System.out.println(path);
        File file = new File(path);
        byte[] buffer = new byte[4096];
        GZIPInputStream gzip;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            gzip = new GZIPInputStream(new FileInputStream(file.toString()));
            int len;
            while ((len = gzip.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            gzip.close();
            baos.close();
        } catch (FileNotFoundException e) {
            System.err.println("Input file " + path + " does not exist.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data = "";
        try {
            data = baos.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // This is always UTF-8 so this should never happen.
            e.printStackTrace();
        }
        return data;
    }
}

Related

  1. uncompress(String inputFile, String toDir)
  2. uncompress(String str)
  3. uncompress(ZipInputStream zipIn)
  4. uncompressDirectory(File zipSource, File target)
  5. uncompressEveryFileFromDirectory(File srcPath, File dstPath)
  6. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  7. unzip(File aFile)
  8. unzip(File archive)
  9. unzip(File archive, File output)