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

(zipfs) ZipPath#getFileName( ) returns inconsistent result

    XMLWordPrintable

Details

    • b121
    • generic
    • generic
    • Verified

    Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      When a Path object is returned by getFileName() for a DIRECTORY entry within a ZipFileSystem, the returned ZipPath object usually includes a trailing slash, but sometimes it does not.

      Calling getFileName() on a WindowsPath object results in a Path object that never contains the File.separator.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create the directory "folder", and a sub directory "folder\subdir".
      Create a zip file "zipfile.zip", containing the above items.

      On Windows, put both "folder" and "zipfile.zip" in "C:\tmp"; on Linux, put it anywhere you like and adapt the path/URI in the Visitor test class.

      Execute the Visitor test class.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      For each test, "folder" and "subdir" should be printed. For the second test, "null" is also expected since the root path has zero elements.

          folder
          subdir

          null
          folder
          subdir

          folder
          subdir

      ACTUAL -
      When visiting directories in the WindowsFileSystem, getFileName() never returns a Path object that contains the File.separator.

      When visiting directories in the ZipFileSystem, getFileName() always includes a trailing slash for "subdir". When "folder" is visited starting from "/", getFileName() includes the trailing slash; but when the visit starts at the path to "folder", no trailing slash is included.

          folder
          subdir

          null
          folder/
          subdir/

          folder
          subdir/


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Visitor extends SimpleFileVisitor<Path> {

      public static void main(String[] args) throws Exception {
      URI uri = new URI("jar:file:/C:/tmp/zipfile.zip");

      Files.walkFileTree(Paths.get("c:\\tmp\\folder"), new Visitor());

      try (FileSystem fs = FileSystems.newFileSystem(uri, new HashMap<String, Object>())) {
      Files.walkFileTree(fs.getPath("/"), new Visitor());
      Files.walkFileTree(fs.getPath("folder"), new Visitor());
      }
      }

      private Visitor() {
      System.out.println();
      }

      public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) {
      System.out.println(path.getFileName());
      return FileVisitResult.CONTINUE;
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      String filename = path.getFileName();
      if (filename != null && filename.endsWith("/"))
          filename = filename.substring(0, filename.length()-1);


      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: