Get a File or URI object for a file inside an archive with Java?

浪子不回头ぞ 提交于 2019-11-29 13:56:50

问题


is it possible to get a File or URI object for a file inside an archive with Java? (zip or jar archive)

Thanks Hemeroc.


回答1:


The jar: protocol is a way to build a URI to a resource in a JAR archive:

jar:http://www.example.com/bar/baz.jar!/path/to/file

See the API docs for JarURLConnection: http://java.sun.com/javase/6/docs/api/java/net/JarURLConnection.html

Between the jar: and !/ can be any URL, including a file: URL.




回答2:


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;
}

for the inputStreamToFile method, google "java inputStream to file", although you might be happy with an InputStream object also, instead of a File object :)




回答3:


For the actual file data, see ZipFile#getInputStream(ZipEntry). The javadocs for that class explain how it is to be used.



来源:https://stackoverflow.com/questions/2049659/get-a-file-or-uri-object-for-a-file-inside-an-archive-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!