集册 Java实例教程 解压罐子

解压罐子

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

508
解压罐子


import java.io.File;/*来 自 nowjava - 时代Java*/

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.URL;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;


public class Main{

    public static List<String> decompressJar(String jarPath,

            String outPath, String[] filter) throws Exception {

        JarFile jarFile = new JarFile(jarPath);
        /** from 
        NowJava.com**/

        List<String> webWars = new ArrayList<String>();

        Enumeration<JarEntry> entrys = jarFile.entries();

        while (entrys.hasMoreElements()) {

            JarEntry entry = entrys.nextElement();

            String name = entry.getName();

            if (name.contains("about.html"))

                continue;

            for (String string : filter) {

                if (name.contains(string)) {

                    String file = extractJarEntry(outPath, jarFile, entry);

                    webWars.add(file);

                }

            }

        }

        jarFile.close();

        return webWars;

    }

    public static String extractJarEntry(String outPath, JarFile jarFile,

            JarEntry entry) throws Exception {

        outPath = outPath.endsWith("/") ? outPath : outPath + '/';

        File file = new File(outPath + entry.getName());

        if (!file.exists()) {

            
展开阅读全文