11

是否可以使用 Java 为存档中的文件获取文件或 URI 对象?(zip 或 jar 存档)

谢谢赫默洛克。

4

3 回答 3

12

jar:协议是一种为 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。

于 2010-01-12T14:40:15.950 回答
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;
}

对于 inputStreamToFile 方法,谷歌“java inputStream to file”,虽然您可能对 InputStream 对象也很满意,而不是 File 对象:)

于 2010-01-12T14:59:11.347 回答
1

有关实际文件数据,请参阅ZipFile#getInputStream(ZipEntry)。该类的 javadocs 解释了如何使用它。

于 2010-01-12T14:41:30.800 回答