Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to check if a URL is a directory  (Read 9923 times)
0 Members and 1 Guest are viewing this topic.
Offline marinepower

Junior Devvie

Projects: 2

« Posted 2009-10-25 23:55:10 »

Hey guys, I'm trying to build a framework that will allow a user to navigate the contents of a signed jar file, and need it to support looking into various subdirectories.  How would one go about figuring out whether or not a given resource is a directory?

I'm getting the initial directory using MapSelection.getClass().getResource("/CremelianWars/Maps/")  which returns a URL.  The File class has a method for checking if it's a directory or not, but a URL doesn't seem to have one.

Offline DzzD
« Reply #1 - Posted 2009-10-26 00:39:08 »

look at the protocol URL.getProtocol() if it is "file" than use (new File(f)).isDirectory()

something like :

if (url.getProtocol().equals("file")) 
 return ( new File(url.getFile() ) ).isDirectory();
return false;

Offline Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #2 - Posted 2009-10-26 00:39:16 »

A URL is the location of a resource, not necessarily a file system. The resource may not have the concept of a directory.

If the URL points to a file, you can just check if the file is a directory. If it points to a JAR, you have to open the JAR, get the corresponding entry, and see if it is a directory. Unfortunately, Java's zip API is quite crappy. I messed around and got the below. While it seems to work, I arrived at it by trial and error because the ZipEntry#isDirectory only seems to return true when the path ends in slash, and ZipFile#getInputStream returns null, and the javadocs don't even tell what the hell that means.

static public boolean isDirectory (URL url) throws IOException {
   String protocol = url.getProtocol();
   if (protocol.equals("file")) {
      return new File(url.getFile()).isDirectory();
   if (protocol.equals("jar")) {
      String file = url.getFile();
      int bangIndex = file.indexOf('!');
      String jarPath = file.substring(bangIndex + 2);
      file = new URL(file.substring(0, bangIndex)).getFile();
      ZipFile zip = new ZipFile(file);
      ZipEntry entry = zip.getEntry(jarPath);
      boolean isDirectory = entry.isDirectory();
      if (!isDirectory) {
         InputStream input = zip.getInputStream(entry);
         isDirectory = input == null;
         if (input != null) input.close();
      return isDirectory;
   throw new RuntimeException("Invalid protocol: " + protocol);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline marinepower

Junior Devvie

Projects: 2

« Reply #3 - Posted 2009-11-17 23:06:42 »

That seems to work, thanks!

Unfortunately, I have just realized that the way I actually read the contents of the folder no longer works when the resources are packaged into a jar.  Before, I would open up a URLConnection on a URL, then read all of the filenames in the folder using a BufferedReader.  However, inside a jar, the following method is not able to obtain the folder as a URL.

URL directory = this.getClass().getResource(mapFolder);
Pages: [1]
  ignore  |  Print  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!