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

(zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE

XMLWordPrintable

    • b67
    • linux
    • Verified

        FULL PRODUCT VERSION :
        java version "1.7.0_45"
        OpenJDK Runtime Environment (IcedTea 2.4.3) (suse-8.24.2-i386)
        OpenJDK Server VM (build 24.45-b08, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        OpenSUSE 12.3 i586

        A DESCRIPTION OF THE PROBLEM :
        It is impossible create a file entry within new ZIP file system with open options CREATE and WRITE if that file not exists (see the test code in attachments).

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the test code and you'll see the NoSuchFileException.


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        dmitry@dm:~/NetBeansProjects/BugZipFs/dist> java -ea -jar BugZipFs.jar
        Exception in thread "main" java.nio.file.NoSuchFileException: test.txt
                at com.sun.nio.zipfs.ZipFileSystem.newFileChannel(ZipFileSystem.java:709)
                at com.sun.nio.zipfs.ZipPath.newFileChannel(ZipPath.java:747)
                at com.sun.nio.zipfs.ZipFileSystemProvider.newFileChannel(ZipFileSystemProvider.java:271)
                at bugzipfs.BugZipFs.main(BugZipFs.java:51)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package bugzipfs;

        import java.io.IOException;
        import java.nio.channels.FileChannel;
        import java.nio.charset.StandardCharsets;
        import java.nio.file.FileSystem;
        import java.nio.file.FileSystems;
        import java.nio.file.FileVisitResult;
        import java.nio.file.Files;
        import java.nio.file.OpenOption;
        import java.nio.file.Path;
        import java.nio.file.SimpleFileVisitor;
        import java.nio.file.StandardOpenOption;
        import java.nio.file.attribute.BasicFileAttributes;
        import java.nio.file.spi.FileSystemProvider;
        import java.util.Collections;
        import java.util.EnumSet;
        import java.util.Map;
        import java.util.Objects;
        import java.util.Set;

        /**
         * @author Dmitry Ovchinnikov
         */
        public class BugZipFs extends SimpleFileVisitor<Path> {

            public static void main(String[] args) throws Exception {
                Set<? extends OpenOption> openOptions = EnumSet.of(
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING);
                FileSystemProvider fsProvider = null;
                for (FileSystemProvider p : FileSystemProvider.installedProviders()) {
                    if (Objects.equals(p.getScheme(), "jar")) {
                        fsProvider = p;
                        break;
                    }
                }
                assert fsProvider != null : "ZIP file system provider does not exist";
                Path tempDir = Files.createTempDirectory("test");
                try {
                    Path normalFile = tempDir.resolve("test.txt");
                    try (FileChannel ch = FileSystems.getDefault().provider().newFileChannel(normalFile, openOptions)) {
                        ch.write(StandardCharsets.UTF_8.encode("Hello!"));
                    }
                    Path zipFile = tempDir.resolve("test.zip");
                    try {
                        Map<String, ?> env = Collections.singletonMap("create", "true");
                        FileSystem fs = fsProvider.newFileSystem(zipFile, env);
                        Path testFile = fs.getPath("test.txt");
                        try (FileChannel ch = fsProvider.newFileChannel(testFile, openOptions)) {
                            ch.write(StandardCharsets.UTF_8.encode("Hello!"));
                        }
                    } finally {
                        Files.deleteIfExists(zipFile);
                        assert Files.notExists(zipFile) : "ZIP file exists";
                    }
                } finally {
                    Files.walkFileTree(tempDir, new BugZipFs()); // Delete the temp dir recursively
                    assert Files.notExists(tempDir) : "Temp directory exists";
                }
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        }

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

              sherman Xueming Shen
              alanb Alan Bateman
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: