Java Gunzip File gunzip(File gzippedFile)

Here you can find the source of gunzip(File gzippedFile)

Description

Unzips a file.

License

Open Source License

Parameter

Parameter Description
gzippedFile A gzipped file.

Exception

Parameter Description
IOException If you screw up.

Return

The gunzipped version of the file.

Declaration

public static File gunzip(File gzippedFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*w  w  w.  ja v a2 s. c  o m*/
 Copyright (C) 2012  Rick Brown

 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Unzips a file.
     * @param gzippedFile A gzipped file.
     * @return The gunzipped version of the file.
     * @throws IOException If you screw up.
     */
    public static File gunzip(File gzippedFile) throws IOException {
        OutputStream out = null;
        GZIPInputStream gzipInputStream = null;
        File gunzippedFile = new File(System.getProperty("java.io.tmpdir")
                + File.separatorChar
                + gzippedFile.getName().replace(".gz", ""));
        try {
            InputStream in = new FileInputStream(gzippedFile);
            gzipInputStream = new GZIPInputStream(in);
            out = new FileOutputStream(gunzippedFile);

            byte[] buf = new byte[1024];
            int len;
            while ((len = gzipInputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (gzipInputStream != null) {
                gzipInputStream.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return gunzippedFile;
    }
}

Related

  1. gunzip(File gzippedFile, File destinationFile)
  2. gUnzip(File srcFile, File destFile)
  3. gunzip(InputStream packedData, OutputStream unpackedData)
  4. gunzip(String compressedStr)