ungzip a gzipped file to a normal file - Java File Path IO

Java examples for File Path IO:GZIP

Description

ungzip a gzipped file to a normal file

Demo Code


//package com.java2s;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.GZIPInputStream;

public class Main {
    /**/*from w w  w. jav  a2 s.c om*/
     * ungzip a gzipped file to a normal file
     * @param gzippedFile
     * @param ungzippedFile
     * @throws IOException
     */
    public static void unzip(File gzippedFile, File ungzippedFile)
            throws IOException {
        GZIPInputStream gis = null;
        FileOutputStream fos = null;
        try {
            gis = new GZIPInputStream(new FileInputStream(gzippedFile));
            fos = new FileOutputStream(ungzippedFile);
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = gis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } finally {
            close(gis);
            close(fos);
        }
    }

    private static void close(Closeable io) {
        if (io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            io = null;
        }
    }
}

Related Tutorials