Java Ungzip File ungzip(File gzip, File toDir)

Here you can find the source of ungzip(File gzip, File toDir)

Description

ungzip

License

Apache License

Declaration

public static File ungzip(File gzip, File toDir) throws IOException 

Method Source Code


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

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 {
    public static File ungzip(File gzip, File toDir) throws IOException {
        toDir.mkdirs();/*from w w  w . java2s  .  c o  m*/
        File out = new File(toDir, gzip.getName());
        GZIPInputStream gin = null;
        FileOutputStream fout = null;
        try {
            FileInputStream fin = new FileInputStream(gzip);
            gin = new GZIPInputStream(fin);
            fout = new FileOutputStream(out);
            copy(gin, fout);
            gin.close();
            fout.close();
        } finally {
            closeQuietly(gin);
            closeQuietly(fout);
        }
        return out;
    }

    private static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyStream(input, output);
        if (count > 2147483647L) {
            return -1;
        }
        return (int) count;
    }

    private static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException localIOException) {
        }
    }

    private static void closeQuietly(InputStream input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException localIOException) {
        }
    }

    private static long copyStream(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024];
        long count = 0L;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. unGzip(File inFile, File outFile)
  2. unGzip(File sourceZipFile, String destFolder, String destFile)
  3. unGzip(final File compressedInputFile, final File outputDir)
  4. unGzip(final File inputFile, final File outputDir)