Java Unzip to Folder unzip(String inFilePath, String outFilePath)

Here you can find the source of unzip(String inFilePath, String outFilePath)

Description

unzip

License

Apache License

Parameter

Parameter Description
inFilePath a parameter
outFilePath a parameter

Declaration

public static String unzip(String inFilePath, String outFilePath) 

Method Source Code

//package com.java2s;
/**/*from  w ww  .  j av  a 2  s .c  o  m*/
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/

import java.io.*;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     *
     * @param inFilePath
     * @param outFilePath
     * @return
     */
    public static String unzip(String inFilePath, String outFilePath) {

        try {

            GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath));

            //  String outFilePath = inFilePath.replace(".gz", "");
            OutputStream out = new FileOutputStream(outFilePath);

            byte[] buf = new byte[1024];
            int len;
            while ((len = gzipInputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            gzipInputStream.close();
            out.close();

            new File(inFilePath).delete();

            return outFilePath;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *
     * @param s
     */
    public static final void close(InputStream s) {
        if (s != null) {
            try {
                s.close();
            } catch (Exception x) {
            }
        }
    }

    /**
     *
     * @param s
     */
    public static final void close(OutputStream s) {
        if (s != null) {
            try {
                s.close();
            } catch (Exception x) {
            }
        }
    }

    /**
     *
     * @param r
     */
    public static final void close(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (Exception x) {
            }
        }
    }

    /**
     *
     * @param w
     */
    public static final void close(Writer w) {
        if (w != null) {
            try {
                w.close();
            } catch (Exception x) {
            }
        }
    }
}

Related

  1. unZip(String baseDir, InputStream is)
  2. unzip(String compressedStr)
  3. unzip(String file, String destinationdir)
  4. unzip(String filePath, String unzipPath)
  5. unzip(String inFilePath)
  6. unzip(String inputZipPath, String destinationDirectory)
  7. unzip(String path, int buffer)
  8. unZip(String pathDest, BufferedInputStream buffInputStream)
  9. unzip(String sourceFile, String destDir)