Java Gunzip File gunzipFile(File file_input, File dir_output)

Here you can find the source of gunzipFile(File file_input, File dir_output)

Description

Gunzip an input file.

License

Open Source License

Parameter

Parameter Description
file_input Input file to gunzip.
dir_output Output directory to contain the ungzipped file (whose name = file_input - ".gz")

Return

1 if succeed, 0 otherwise.

Declaration

public static int gunzipFile(File file_input, File dir_output) 

Method Source Code

//package com.java2s;
/*===============================================================================
 * Copyright (c) 2010-2015 University of Massachusetts.  All Rights Reserved.
 *
 * Use of the RankLib package is subject to the terms of the software license set 
 * forth in the LICENSE file included with this software, and also available at
 * http://people.cs.umass.edu/~vdang/ranklib_license.html
 *===============================================================================
 *//* w w  w .  j  av  a 2 s  .  c o m*/

import java.io.*;

import java.util.zip.GZIPInputStream;

public class Main {
    public static final int BUF_SIZE = 51200;

    /**
     * Gunzip an input file.
     * @param file_input   Input file to gunzip.
     * @param dir_output   Output directory to contain the ungzipped file (whose name = file_input - ".gz")
     * @return 1 if succeed, 0 otherwise.
     */
    public static int gunzipFile(File file_input, File dir_output) {
        // Create a buffered gzip input stream to the archive file.
        GZIPInputStream gzip_in_stream;
        try {
            FileInputStream in = new FileInputStream(file_input);
            BufferedInputStream source = new BufferedInputStream(in);
            gzip_in_stream = new GZIPInputStream(source);
        } catch (IOException e) {
            System.out.println("Error in gunzipFile(): " + e.toString());
            return 0;
        }

        // Use the name of the archive for the output file name but
        // with ".gz" stripped off.
        String file_input_name = file_input.getName();
        String file_output_name = file_input_name.substring(0,
                file_input_name.length() - 3);

        // Create the decompressed output file.
        File output_file = new File(dir_output, file_output_name);

        // Decompress the gzipped file by reading it via
        // the GZIP input stream. Will need a buffer.
        byte[] input_buffer = new byte[BUF_SIZE];
        int len = 0;
        try {
            // Create a buffered output stream to the file.
            FileOutputStream out = new FileOutputStream(output_file);
            BufferedOutputStream destination = new BufferedOutputStream(
                    out, BUF_SIZE);

            //Now read from the gzip stream, which will decompress the data,
            //and write to the output stream.
            while ((len = gzip_in_stream.read(input_buffer, 0, BUF_SIZE)) != -1)
                destination.write(input_buffer, 0, len);
            destination.flush(); // Insure that all data is written to the output.
            out.close();
        } catch (IOException e) {
            System.out.println("Error in gunzipFile(): " + e.toString());
            return 0;
        }

        try {
            gzip_in_stream.close();
        } catch (IOException e) {
            return 0;
        }
        return 1;
    }

    /**
     * Read the content of a file.
     * @param filename The file to read.
     * @param encoding The encoding of the file.
     * @return The content of the input file.
     */
    public static String read(String filename, String encoding) {
        BufferedReader in;
        String content = "";
        try {
            in = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filename), encoding));
            char[] newContent = new char[40960];
            int numRead = -1;
            while ((numRead = in.read(newContent)) != -1) {
                content += new String(newContent, 0, numRead);
            }
            in.close();
        } catch (Exception e) {
            content = "";
        }
        return content;
    }

    /**
     * Write a text to a file.
     * @param filename The output filename.
     * @param encoding The encoding of the file.
     * @param strToWrite The string to write.
     * @return TRUE if the procedure succeeds; FALSE otherwise.
     */
    public static boolean write(String filename, String encoding,
            String strToWrite) {
        BufferedWriter out = null;
        try {

            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filename), encoding));
            out.write(strToWrite);
            out.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

Related

  1. gUnzip(File srcFile, File destFile)
  2. gunzip(InputStream packedData, OutputStream unpackedData)
  3. gunzip(String compressedStr)
  4. gunzip(String inFile, String outFile)
  5. gunzipFile(File baseDir, File gzFile)
  6. gunzipFile(File inputGZippedFile, File outputFile)
  7. ungzip(byte[] bytes)