[英]Get a File or URI object for a file inside an archive with Java?
是否可以使用Java獲取存檔內的文件的文件或URI對象? (zip或jar檔案)
謝謝Hemeroc。
jar:
protocol是一種在JAR存檔中為資源構建URI的方法:
jar:http://www.example.com/bar/baz.jar!/path/to/file
請參閱JarURLConnection的API文檔: http : //java.sun.com/javase/6/docs/api/java/net/JarURLConnection.html
jar:
和!/
可以是任何URL,包括file:
URL。
public List<File> getFilesInJar(String jarName){
List<File> result = new ArrayList<File>();
File jarFile = new File(jarName);
JarInputStream jarFile = new JarInputStream(new FileInputStream(jarFile));
JarEntry jarEntry;
while ((jarEntry = jarFile.getNextJarEntry()) != null) {
result.add(inputStreamToFile(jarFile.getInputStream(jarEntry)));
}
return result;
}
對於inputStreamToFile方法,谷歌“java inputStream to file”,雖然你也可能對InputStream對象感到滿意,而不是File對象:)
有關實際的文件數據,請參閱ZipFile#getInputStream(ZipEntry) 。 該類的javadoc解釋了如何使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.