/*
 ----- BEGIN LICENSE BLOCK -----
 This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0.
 If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 ----- END LICENSE BLOCK -----
 */
package org.pharmgkb.common.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;


/**
 * Utility methods for working with {@link Path}s.
 *
 * @author Mark Woon
 */
public final class PathUtils {

  /**
   * Private constructor to prevent instantiation of utility class.
   */
  private PathUtils() {
  }


  /**
   * Gets the name of the file.
   * Does not validate if {@code file} is a regular file.
   */
  public static @Nonnull String getFilename(@Nonnull Path file) {
    Preconditions.checkNotNull(file);

    return file.getName(file.getNameCount() - 1).toString();
  }

  /**
   * Gets the base name of the file (i.e. file name without the extension).
   * Does not validate if {@code file} is a regular file.
   */
  public static @Nonnull String getBaseFilename(@Nonnull Path file) {
    Preconditions.checkNotNull(file);

    String fileName = getFilename(file);
    int idx = fileName.lastIndexOf(".");
    if (idx == -1) {
      return fileName;
    }
    return fileName.substring(0, idx);
  }


  /**
   * Converts a resource file name into a {@link Path}.
   *
   * @param filename a relative filename (e.g. {@code org/pharmgkb/common/file.txt})
   */
  public static @Nonnull Path getPathToResource(@Nonnull String filename) {

    Preconditions.checkNotNull(filename);
    URL url = PathUtils.class.getClassLoader().getResource(filename);
    if (url == null) {
      throw new IllegalArgumentException("No such resource: " + filename);
    }
    try {
      if (url.getProtocol().equalsIgnoreCase("jar")) {
        try {
          FileSystems.newFileSystem(url.toURI(), Collections.emptyMap());
        } catch (IOException ex) {
          throw new IllegalStateException("Unable to create zip/jar filesystem", ex);
        }
      }
      return Paths.get(url.toURI());
    } catch (URISyntaxException ex) {
      // should never happen
      throw new IllegalStateException("Filename '" + filename + "' translated to invalid URI (" + url + ")", ex);
    }
  }
}

@@ -313,32 +313,53 @@
 io.IOException;%0A
+import java.net.URI;%0A
 import java.net.
@@ -399,16 +399,66 @@
 et.URL;%0A
+import java.nio.file.FileSystemNotFoundException;%0A
 import j
@@ -2035,24 +2035,53 @@
 %7D%0A    try %7B%0A
+      URI uri = url.toURI();%0A
       if (ur
@@ -2159,19 +2159,19 @@
 Systems.
-new
+get
 FileSyst
@@ -2175,25 +2175,128 @@
 ystem(ur
-l.toURI()
+i);%0A        %7D catch (FileSystemNotFoundException ex) %7B%0A          try %7B%0A            FileSystems.newFileSystem(uri
 , Collec
@@ -2314,32 +2314,34 @@
 Map());%0A        
+ 
+ 
 %7D catch (IOExcep
@@ -2339,36 +2339,39 @@
  (IOException ex
+2
 ) %7B%0A
+  
           throw 
@@ -2433,27 +2433,40 @@
 esystem%22, ex
+2
 );%0A
+          %7D%0A
         %7D%0A  
@@ -2496,25 +2496,17 @@
 s.get(ur
-l.toURI()
+i
 );%0A    %7D

Make sure filesystem is only created once.