????????ClassLoader???Jar????????

    /**
     * ????????Jar???ж??????? Jar???????????????File???????????Stream?????????
     * 
     * @author lihzh
     * @throws URISyntaxException
     * @throws IOException
     * @data 2012-4-11 ????11:07:58
     */
    @Test
    public void testGetFileFromJarInClassPath() throws URISyntaxException??
            IOException {
        Enumeration<URL> urls = this.getClass().getClassLoader().getResources("conf/test.properties");
        URL url = this.getClass().getClassLoader().getResource("conf/test.properties");
        Assert.assertTrue(urls.hasMoreElements());
        Assert.assertNotNull(url);
        // ????????????÷????·??????????????????“/” ??????????????????????????????????????????
        // ?????Class??getResource???????????????resolveName????????????????·???????????ClassLoader?????
        // getResource??getResources???????????????????????
        URL clzURL = this.getClass().getResource("/conf/test.properties");
        URL nullURL = this.getClass().getResource("conf/test.properties");
        Assert.assertNotNull(clzURL);
        Assert.assertNull(nullURL);
        URL thisClzURL = this.getClass().getResource("");
        Assert.assertNotNull(thisClzURL);
        // ?????????????
        InputStream is = this.getClass().getResourceAsStream("/conf/test.properties");
        Properties props = new Properties();
        props.load(is);
        Assert.assertTrue(props.containsKey("test.key"));
        Assert.assertEquals("thisIsValue"?? props.getProperty("test.key"));
    }

???????Jar???·???μ????????

    /**
     * ??ClassPath?е?Jar????????????μ????????
     * 
     * @author lihzh
     * @throws IOException 
     * @data 2012-4-13 ????10:22:24
     */
    @Test
    public void testGetFilesFromJarInClassPathWithDirPath() throws IOException {
        String dirPath = "conf/";
        URL url = this.getClass().getClassLoader().getResource(dirPath);
        Assert.assertNotNull(url);
        String urlStr = url.toString();
        // ???!/ ????????????
        String jarPath = urlStr.substring(0?? urlStr.indexOf("!/") + 2);
        URL jarURL = new URL(jarPath);
        JarURLConnection jarCon = (JarURLConnection) jarURL.openConnection();
        JarFile jarFile = jarCon.getJarFile();
        Enumeration<JarEntry> jarEntrys = jarFile.entries();
        Assert.assertTrue(jarEntrys.hasMoreElements());
        Properties props = new Properties();
        while (jarEntrys.hasMoreElements()) {
            JarEntry entry = jarEntrys.nextElement();
            // ?????ж?·???????????????Spring??Ant-Style?????·???????????????
            String name = entry.getName();
            if (name.startsWith(dirPath) && !entry.isDirectory()) {
                // ?????????????
                InputStream is = this.getClass().getClassLoader().getResourceAsStream(name);
                Assert.assertNotNull(is);
                props.load(is);
            }
        }
        Assert.assertTrue(props.containsKey("test.key"));
        Assert.assertEquals("thisIsValue"?? props.getProperty("test.key"));
        Assert.assertTrue(props.containsKey("test.key.two"));
        Assert.assertEquals("thisIsAnotherValue"?? props.getProperty("test.key.two"));
    }

???????????ClassPath?μ?Jar????????????????????????JarFile??????ɡ?·???????·???????????????url.getConnection???????????????????