Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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 3451 times)
0 Members and 1 Guest are viewing this topic.
Offline marinepower

Junior Member


Projects: 2



« Posted 2009-10-26 00: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.

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

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

something like :

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

Offline Nate

JGO Kernel


Medals: 129
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2009-10-26 01: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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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 Member


Projects: 2



« Reply #3 - Posted 2009-11-18 00: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.

1  
URL directory = this.getClass().getResource(mapFolder);
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (47 views)
2014-04-15 18:08:23

BurntPizza (43 views)
2014-04-15 03:46:01

UprightPath (59 views)
2014-04-14 17:39:50

UprightPath (41 views)
2014-04-14 17:35:47

Porlus (57 views)
2014-04-14 15:48:38

tom_mai78101 (80 views)
2014-04-10 04:04:31

BurntPizza (139 views)
2014-04-08 23:06:04

tom_mai78101 (239 views)
2014-04-05 13:34:39

trollwarrior1 (199 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!