Java Ungzip File unGzip(final File compressedInputFile, final File outputDir)

Here you can find the source of unGzip(final File compressedInputFile, final File outputDir)

Description

Ungzip an input file into an output file.

License

Apache License

Parameter

Parameter Description
compressedInputFile the input .gz file
outputDir the output directory file.

Exception

Parameter Description

Return

File with the ungzipped content.

Declaration

private static File unGzip(final File compressedInputFile, final File outputDir) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.zip.GZIPInputStream;

public class Main {
    /**//w w w.  j a v a  2  s  .c om
     * Ungzip an input file into an output file.
     * <p>
     * The output file is created in the output folder, having the same name
     * as the input file, minus the '.gz' extension.
     *
     * @param compressedInputFile     the input .gz file
     * @param outputDir     the output directory file.
     * @throws java.io.IOException
     * @throws java.io.FileNotFoundException
     *
     * @return File with the ungzipped content.
     */
    private static File unGzip(final File compressedInputFile, final File outputDir) throws IOException {
        String uncompressedOutputFileName = null;
        if (compressedInputFile.getName().endsWith(".gz")) {
            uncompressedOutputFileName = compressedInputFile.getName().substring(0,
                    compressedInputFile.getName().length() - 3);
        } else if (compressedInputFile.getName().endsWith(".tgz")) {
            uncompressedOutputFileName = compressedInputFile.getName().substring(0,
                    compressedInputFile.getName().length() - 3) + "tar";
        } else {
            throw new IllegalArgumentException("Compressed File:[" + compressedInputFile.getName()
                    + "] Archive Type in unknown to this Utility Function!");
        }

        // Un-Compress.
        final File outputFile = new File(outputDir, uncompressedOutputFileName);

        final GZIPInputStream in = new GZIPInputStream(new FileInputStream(compressedInputFile));
        final FileOutputStream out = new FileOutputStream(outputFile);

        for (int c = in.read(); c != -1; c = in.read()) {
            out.write(c);
        }

        in.close();
        out.close();

        return outputFile;
    }
}

Related

  1. ungzip(File gzip, File toDir)
  2. unGzip(File inFile, File outFile)
  3. unGzip(File sourceZipFile, String destFolder, String destFile)
  4. unGzip(final File inputFile, final File outputDir)
  5. ungzip(InputStream ins)
  6. ungzip(String gzipFile, String newFile)
  7. ungzipFile(String inputFileName, String outputFileName)