Java Unzip to Folder unzip(String archive, String out_dir)

Here you can find the source of unzip(String archive, String out_dir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(String archive, String out_dir) throws Exception 

Method Source Code

//package com.java2s;
/**/*from  w w  w.  j  a  v a2s . c o  m*/
 * ##################
 * # Tilde Launcher #
 * ##################
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Riccardo B.
 * @version 0.1
 */

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzip(String archive, String out_dir) throws Exception {

        ZipFile zipfile = new ZipFile(new File(archive));
        Enumeration<? extends ZipEntry> en = zipfile.entries();

        BufferedInputStream ins = null;
        FileOutputStream outs = null;
        File outFile = null;

        while (en.hasMoreElements()) {
            try {

                ZipEntry entry = (ZipEntry) en.nextElement();

                ins = new BufferedInputStream(zipfile.getInputStream(entry));

                outFile = new File(out_dir + entry.getName());

                new File(outFile.getParent()).mkdirs();

                if (entry.isDirectory()) {
                    new File(outFile.getPath()).mkdirs();
                    continue;
                }

                outs = new FileOutputStream(outFile);

                byte[] buffer = new byte[1024 * 100];
                int read;
                while ((read = ins.read(buffer)) > 0) {
                    outs.write(buffer, 0, read);

                }

                ins.close();
                outs.close();
            } catch (Exception e) {

                if (ins != null)
                    ins.close();
                if (outs != null)
                    outs.close();
                break;
            } finally {
                if (ins != null)
                    ins.close();
                if (outs != null)
                    outs.close();
            }
        }
        zipfile.close();
    }

    public static void mkdirs(String f) {
        new File(f).mkdirs();
    }
}

Related

  1. unZip(String baseDir, InputStream is)
  2. unzip(String compressedStr)
  3. unzip(String file, String destinationdir)
  4. unzip(String filePath, String unzipPath)