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

前端 未结 3 1637
失恋的感觉
失恋的感觉 2020-12-31 10:56

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

Thanks Hemeroc.

3条回答
  •  离开以前
    2020-12-31 11:38

    public List getFilesInJar(String jarName){    
      List result = new ArrayList();
      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 :)

提交回复
热议问题