Java Gunzip File gUnzip(File srcFile, File destFile)

Here you can find the source of gUnzip(File srcFile, File destFile)

Description

GUnzips a source File to a destination File.

License

Open Source License

Parameter

Parameter Description
srcFile the source file.
destFile the destination file.

Exception

Parameter Description
IOException error handling the files.

Declaration

public static void gUnzip(File srcFile, File destFile) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "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 {
    /**/* ww w  .  j  a  va  2  s .c  om*/
     * GUnzips a source File to a destination File.
     *
     * @param srcFile the source file.
     * @param destFile the destination file.
     * @throws IOException error handling the files.
     */
    public static void gUnzip(File srcFile, File destFile) throws IOException {

        FileOutputStream fos = null;
        GZIPInputStream zIn = null;
        InputStream fis = null;

        try {
            fos = new FileOutputStream(destFile);
            fis = new FileInputStream(srcFile);
            zIn = new GZIPInputStream(fis);

            copy(zIn, fos);

        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (zIn != null) {
                zIn.close();
            }
        }
    }

    /**
     * Copies from the {@code InputStream} into the {@code OutputStream}.
     *
     * @param inputStream the {@code GZIPInputStream}.
     * @param outputStream the {@code OutputStream}.
     * @throws IOException error handling files.
     */
    private static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {

        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            outputStream.write(buffer, 0, count);
            count = inputStream.read(buffer, 0, buffer.length);
        } while (count != -1);
    }
}

Related

  1. gunzip(File gzippedFile)
  2. gunzip(File gzippedFile, File destinationFile)
  3. gunzip(InputStream packedData, OutputStream unpackedData)
  4. gunzip(String compressedStr)
  5. gunzip(String inFile, String outFile)
  6. gunzipFile(File baseDir, File gzFile)