Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8146754

(zipfs) ZipPath.relativize() returns wrong result for path ending with slash /

XMLWordPrintable

    • b121
    • generic
    • generic
    • Verified

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When a ZipFileSystem is created and queried for a path inside the zip, and if the query path ends with a slash then the returned path ends with a slash, too.
      When method relativize() is called for a child of this path then the result is wrong: it starts with ../<lastPathComponent>


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a small zip file "smallzip.zip" containing:
      tmp/path/text.txt
      (The file text.txt can be empty.)

      On Windows, put the file in C:\tmp, on linux put it anywhere you like and adapt the URI in the test method.
      Execute this code:

      public static void main(String[] args) throws Exception {
      URI uri = new URI("jar:file:/C:/tmp/smallzip.zip");
      FileSystem fileSystem = FileSystems.newFileSystem(uri, new HashMap<String, Object>());
      Path path = fileSystem.getPath("/tmp/path");
      Path pathWithSlash = fileSystem.getPath("/tmp/path/");

      Path textFilePath = fileSystem.getPath("/tmp/path/text.txt");
      Path relativized = path.relativize(textFilePath);
      Path relativized1 = pathWithSlash.relativize(textFilePath);

      System.out.println("relativized: " + relativized);
      System.out.println("relativized1: " + relativized1);
      }

      The output will be:
      relativized: text.txt
      relativized1: ../path/text.txt

      Please note that everything works fine if the path with the slash at the end has to be corrected in some other way. You can see this by inserting an extra slash into pathWithSlash, like this:
      Path pathWithSlash = fileSystem.getPath("/tmp//path/");
      (2 slashes after "/tmp").

      When you execute the changed code then everything is fine, the output is:
      relativized: text.txt
      relativized1: text.txt

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This should be printed on the console:

      relativized: text.txt
      relativized1: text.txt
      ACTUAL -
      This is printed on the console:

      relativized: text.txt
      relativized1: ../path/text.txt

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) throws Exception {
      URI uri = new URI("jar:file:/C:/tmp/smallzip.zip");
      FileSystem fileSystem = FileSystems.newFileSystem(uri, new HashMap<String, Object>());
      Path path = fileSystem.getPath("/tmp/path");
      Path pathWithSlash = fileSystem.getPath("/tmp/path/");

      Path textFilePath = fileSystem.getPath("/tmp/path/text.txt");
      Path relativized = path.relativize(textFilePath);
      Path relativized1 = pathWithSlash.relativize(textFilePath);

      System.out.println("relativized: " + relativized);
      System.out.println("relativized1: " + relativized1);
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Everything is ok if the slash is removed by the caller.

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: