集册 Java实例教程 从路径获取罐子

从路径获取罐子

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

483
从路径获取罐子
/* from 时代Java公众号 - nowjava.com*/

//package com.nowjava;

import javax.swing.*;

import java.io.File;

import java.io.FileFilter;

import java.io.IOException;

import java.net.URISyntaxException;

import java.net.URL;

import java.net.URLDecoder;

import java.util.*;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;


public class Main {

    public static List<URL> getJarsFromPath(Class clazz, String path)

            throws URISyntaxException, IOException {

        URL dirURL = clazz.getClassLoader().getResource(path);

        if (dirURL != null && dirURL.getProtocol().equals("file")) {

            /* A file path: easy enough */

            File[] jars = new File(dirURL.toURI())

                    .listFiles(new FileFilter() {
                    /*来自 
                     N  o w  J a v a . c o m*/

                        @Override

                        public boolean accept(File pathname) {

                            return pathname.getName().toLowerCase()

                                    .endsWith(".jar");

                        }

                    });

            List<URL> jarFiles = new ArrayList<>();

            for (File f : jars) {

                jarFiles.add(f.toURI().toURL());

            }

            return jarFiles;

        }


        if (dirURL == null) {

            /*

             * In case of a jar file, we can't actually find a directory.

             * Have to assume the same jar as clazz.

             */

            String me = clazz.getName().replace(".", "/") + ".class";

            dirURL = clazz.getClassLoader().getResource(me);

        }

        if (dirURL.getProtocol().equals("jar")) {

            /* A JAR path */

            String jarPath = dirURL.getPath().substring(5,

                    dirURL.getPath().indexOf("!")); //strip out only the JAR file

            JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));

            Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar

            List<URL> result = new ArrayList<>(); //avoid duplicates in case it is a subdirectory

            while (entries.hasMoreElements()) {

                JarEntry jarEntry = entries.nextElement();

                String name = jarEntry.getName();

                if (name.startsWith(path)

                        && name.toLowerCase().endsWith(".jar")) { //filter according to the path\

                    String jarFile = name.replaceAll("\\.", "/");

                    jarFile = jarFile.substring(0, jarFile.length()

                            -
展开阅读全文