Working With Resources in Other File Systems

Most of the API in the IResource hierarchy works the same way regardless of what kind of file system the resources are stored in. However, there are some particular APIs that you need to avoid when working with resources in other file systems, because they are only designed to work with the local file system.

In particular, the IResource.getLocation method is specified to return the local file system path of the resource. If the resource is in some other file system, then this method is not applicable and will return null. It is better to use the method getLocationURI instead, which works regardless of what kind of file system the resource is stored in.

Similarly, the method IProjectDescription getLocation and setLocation methods should be avoided because they are only effective in the local file system. The URI-based location methods should be used instead.

Local Caching

Say you are working with resources that are not in the local file system, but you really need a local file. For example, you may be using a library that has a dependency on java.io.File). In this case you can use the IFileStore.toLocalFile method to obtain a local copy of the file. Note that this will only cause one file or directory to be cached locally, rather than an entire directory tree. Here is an example of using local caching to open a zip file on an IFileStore:

   IFileStore store = ...;//some file store
   java.io.File file = store.toLocalFile(EFS.NONE, null);
   if (file == null) {
      //we are not a local file store, so we need to cache a local copy
      file = store.toLocalFile(EFS.CACHE, null);
   }
   java.util.zip.ZipFile zip = new java.util.ZipFile(file);

Linking to Other File Systems

You can use linked resources to create projects that draw together resources from multiple file systems. Simply use the method IFile.createLink(URI, int, IProgressMonitor) or IFolder.createLink(URI, int, IProgressMonitor) to create a resource in an existing project whose contents are stored in another location in an arbitrary file system. You can even create links below other linked resources to create arbitrary resource trees drawing from many different file systems. Here is a simple example that creates a sibling linked file that shares the same file system location as the source:

   IFile source = ...;//some source file
   IFile link = source.getParent().getFile(new Path(source.getName() + ".link"));
   link.createLink(source.getLocationURI(), IResource.NONE, null);