java 如何列出jar包中jar包里的所有文件和目錄?
問題描述
如這樣一個目錄/Users/xxx/IdeaProjects/abc/web/target/web.jar!/BOOT-INF/lib/rest-0.0.1.jar!/com/tg/tiny/Users/xxx/IdeaProjects/abc/web/target/web.jar這個jar包下的文件目錄可以這樣得到
JarFile localJarFile = new JarFile(new File('/Users/xxx/IdeaProjects/abc/web/target/web.jar'));Enumeration<JarEntry> entries = localJarFile.entries();while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); System.out.println(jarEntry.getName());}
那么這個web.jar里的rest-0.0.1.jar下的文件目錄如何得到?
問題解答
回答1:URL url = new URL('jar', null, 0, 'file:/Users/xxx/IdeaProjects/web/target/web.jar!/BOOT-INF/lib/rest.jar');URLConnection con = url.openConnection();if (con instanceof JarURLConnection) { JarURLConnection result = (JarURLConnection) con; JarInputStream jarInputStream = new JarInputStream(result.getInputStream()); JarEntry entry; while ((entry = jarInputStream.getNextJarEntry()) != null) {System.out.println(entry.getName()); }}回答2:
jar包是個文件。不是目錄。需要通過classloader的getResourceAsStream()。
package edu.hxraid; import java.io.*; public class Resource { public void getResource() throws IOException{ //返回讀取指定資源的輸入流 InputStream is=this.getClass().getResourceAsStream('/resource/res.txt'); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String s=''; while((s=br.readLine())!=null) System.out.println(s); } }
詳細使用:http://www.myexception.cn/pro...
相關文章:
1. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. Span標簽3. css - 求推薦適用于vue2的框架 像bootstrap這種類型的4. docker-machine添加一個已有的docker主機問題5. java - Collections類里的swap函數,源碼為什么要新定義一個final的List型變量l指向傳入的list?6. css - 關于div自適應問題,大家看圖吧,說不清7. android新手一枚,android使用httclient獲取服務器端數據失敗,但是用java工程運行就可以成功獲取。8. angular.js使用$resource服務把數據存入mongodb的問題。9. redis啟動有問題?10. SessionNotFoundException:會話ID為null。調用quit()后使用WebDriver嗎?(硒)
