集册 Java实例教程 提取给定的罐子

提取给定的罐子

欢马劈雪     最近更新时间:2020-01-02 10:19:05

493
将给定的jar文件提取到指定目录。

/*

 * 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.nowjava;

import java.io.*;
/*
N o w J a v a . c o m - 时  代  Java 提供
*/

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()
        /*
         from 时代Java公众号 - nowjava.com 
        */

                + 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 
展开阅读全文