Extracts the given jar-file to the specified directory. - Java Reflection

Java examples for Reflection:Jar

Description

Extracts the given jar-file to the specified directory.

Demo Code

/*/*www .jav a  2  s  . co  m*/
 * Created on 24-Feb-2004 at 16:35:32.
 *
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish 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 J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */
//package com.java2s;
import java.io.*;
import java.util.Enumeration;
import java.util.jar.*;

import java.util.zip.ZipFile;

public class Main {
    /**
     * Extracts the given jar-file to the specified directory.
     * The target directory will be cleard before the jar-file will be extracted.
     * 
     * @param jarFile The jar file which should be unpacked
     * @param targetDir The directory to which the jar-content should be extracted.
     * @throws FileNotFoundException when the jarFile does not exist
     * @throws IOException when a file could not be written or the jar-file could not read.
     */
    public static void unjar(File jarFile, File targetDir)
            throws FileNotFoundException, IOException {
        // clear target directory:
        if (targetDir.exists()) {
            targetDir.delete();
        }
        // create new target directory:
        targetDir.mkdirs();
        // read jar-file:
        String targetPath = targetDir.getAbsolutePath()
                + File.separatorChar;
        byte[] buffer = new byte[1024 * 1024];
        JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);
        Enumeration e = input.entries();
        for (; e.hasMoreElements();) {
            JarEntry entry = (JarEntry) e.nextElement();
            String path = targetPath + entry.getName();
            File file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileOutputStream out = new FileOutputStream(file);
            InputStream in = input.getInputStream(entry);
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            out.close();
        }

    }
}

Related Tutorials