2

I've been using Java 7's ZipFS support.

https://gist.github.com/stain/5591420

shows the behaviour, which I find a bit odd. Basically you can create a ZIP file system, make a file with a given name, and then also make a folder with the same name.

The reason for this seems to be that internally the folder gets "/" appended to its name - however this new name is not returned, therefore you end up in a strange situation where Files.isDirectory() returns false immediately after a successful Files.createDirectory().

try (FileSystem fs = tempZipFS()) {
        Path folder = fs.getPath("folder");
        Files.createFile(folder);
        assertTrue(Files.isRegularFile(folder));
        assertFalse(Files.isDirectory(folder)); 
//        try {
            Files.createDirectory(folder);
//        } catch (FileAlreadyExistsException ex) {              
//                Is not thrown!
//        }

        // but a second createDirectory() fails correctly
        try {
            Files.createDirectory(folder);
        } catch (FileAlreadyExistsException ex) {               
        }

        // Look, it's both a file and folder! 
        Path child = folder.resolve("child");
        Files.createFile(child);

        // Can this be tested?
        assertTrue(Files.isRegularFile(folder));
        // Yes, if you include the final /
        assertTrue(Files.isDirectory(fs.getPath("folder/")));
        // But not the parent
//          assertTrue(Files.isDirectory(child.getParent()));
        // Or the original Path
//          assertTrue(Files.isDirectory(folder));
    }

So as long as you have the "/" as the suffix, you can even work with both, and that's how they are listed if you do a directory listing of the root.

Now the ZIP format itself allows this as it only deals with entries in a ZIP file (even allowing multiple entries with the same name), however normal use of a "FileSystem" would normally not allow multiple entries with the same name ; as can be seen when I try to create the folder twice.

The produced ZIP file can be browsed correctly with infozip, 7Zip and Windows 8; but trying to unzip will obviously fail because the native file system don't support such duality.

So is this a feature, bug or something in between?

  • Verified also with Java 8 SDK b89 on Windows 8/x64. – Stian Soiland-Reyes May 16 '13 at 14:00
  • Does it make a difference if you assign the result back using: "folder = Files.createDirectory(folder)"? FileAlreadyExistsException is javadoc'd as an optional specific exception, although I am not sure whether that means you should catch IOException to be sure or not. – Peter May 17 '13 at 03:55
  • I must admit I did not know about the return from createDirectory, but it does not make any difference, as it returns "folder" instead of "folder/" - which is what you would expect if the duality was officially supported. – Stian Soiland-Reyes May 20 '13 at 15:41
  • You would also think that "optional" means "Either fully supported or not at all" - not "Supported in certain order of operations". – Stian Soiland-Reyes May 20 '13 at 15:42
  • This "bug" seems to be fixed in JDK9. where a FileAlreadyExistsException is correctly thrown on first attempt. – Stian Soiland-Reyes Apr 18 '18 at 13:24

0 Answers0