Java Unzip File decompressFile(final File toDecompress, final File destinationFile)

Here you can find the source of decompressFile(final File toDecompress, final File destinationFile)

Description

Decompreses one gzipped file into another file.

License

Apache License

Parameter

Parameter Description
toDecompress a parameter
destinationFile a parameter

Exception

Parameter Description
IOException if there was a problem.

Declaration

public static void decompressFile(final File toDecompress, final File destinationFile) throws IOException 

Method Source Code


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

import java.io.*;
import java.util.zip.GZIPInputStream;

public class Main {
    public static final int BUFF_SIZE = 2048;

    /**/*w ww.j  a  v  a  2s.c  o m*/
     * Decompreses one gzipped file into another file.
     *
     * @param toDecompress
     * @param destinationFile
     * @throws IOException if there was a problem.
     */
    public static void decompressFile(final File toDecompress, final File destinationFile) throws IOException {

        if (destinationFile.exists()) {
            throw new IOException("Refusing to overwrite an existing file.");
        }

        InputStream istream = null;
        OutputStream ostream = null;
        try {

            ostream = new BufferedOutputStream(new FileOutputStream(destinationFile), BUFF_SIZE);
            istream = getCompressedFileAsStream(toDecompress);
            transferStream(istream, ostream);
        } finally {
            if (istream != null) {
                istream.close();
            }
            if (ostream != null) {
                ostream.close();
            }
        }
    }

    /**
     * gets an InputStream from a file that is gzip compressed.
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static InputStream getCompressedFileAsStream(final File file) throws IOException {
        return new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    }

    /**
     * Transfers an InputStream to an OutputStream it is up to the job of the caller to close the streams.
     *
     * @param istream
     * @param ostream
     * @throws IOException
     */
    protected static void transferStream(final InputStream istream, final OutputStream ostream) throws IOException {
        final byte[] inBuf = new byte[BUFF_SIZE];
        int readBytes = istream.read(inBuf);
        while (readBytes >= 0) {
            ostream.write(inBuf, 0, readBytes);
            readBytes = istream.read(inBuf);
        }
    }
}

Related

  1. decompress(String fileName, String destinationFolder)
  2. decompress(String filepath, File outpathdir, String includes)
  3. decompress(String zipFile, String targetPath)
  4. decompresses(File file, File destinationDirectory)
  5. decompressFile(File destFile, ZipInputStream zis)
  6. decompressFile(String inName)
  7. decompressGzip(File src, File trgt)
  8. uncompress(ByteArrayInputStream bais)
  9. uncompress(File zipFile, File folder)