简体   繁体   中英

JAR - Listing files into a folder

I would like to get a list of file contained in a directory which is in a jar package. I have an "images" folder, within it I have an Images class that should load all images from that directory.

In the past i used the MyClass.class.getResourceAsStream("filename"); to read files, but how do I read a directory?

This is what I tried:

System.out.println(Images.class.getResource("").getPath());
System.out.println(new File(Images.class.getResource("").getPath()).listFiles());

I tried with Images.class.getResource because I have to work with File and there isn't a constructor that accepts an InputStream .

The code produces

file:/home/k55/Java/MyApp/dist/Package.jar!/MyApp/images/
null

So it is finding the folder which I want to list files from, but it is not able to list files.

I've read on other forums that in fact you can't use this method for folders in a jar archive, so how can I accomplish this?

Update: if possible, i would like to read files without having to use the ZipInputStream

You can't do that easily.

What you need to do:

  1. Get the path of the jar file. Images.class.getResource("/something/that/exists").getPath()
  2. Strip "!/something/that/exists".
  3. Use Zip File System to browse the Jar file. It's a little bit of hacking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM