1

The Zip filesystem doesn't copy file attributes when using the Java nio Files.copy method with StandardCopyOption.COPY_ATTRIBUTES. Is it supposed to?

The following fully working example code demonstrates the issue. It copies two files into a zip file: one normal, the other read-only. If you then list the zip file (e.g. using 7-zip) you'll see they are both normal, not read-only.

public static void main(String[] args) throws Exception {
    Path tmpdir = Files.createTempDirectory(null);
    createFiles(tmpdir);
    createZip(tmpdir);
}

private static void createFiles(Path tmpdir) throws IOException {
    Files.write(tmpdir.resolve("a.txt"), Collections.singleton("Hello, world! (a)"));
    Files.write(tmpdir.resolve("b.txt"), Collections.singleton("Hello, world! (b)"));
    Files.setAttribute(tmpdir.resolve("b.txt"), "dos:readonly", true);
}

private static void createZip(Path dir) throws IOException
{
    Path zip = dir.resolve("data.zip");
    URI uri = URI.create("jar:" + zip.toUri());
    try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.singletonMap("create", "true"))) {
        for (Path path : Files.newDirectoryStream(dir))
            if (!path.equals(zip)) {
                String name = path.getFileName().toString();
                Files.copy(path, fs.getPath(name), StandardCopyOption.COPY_ATTRIBUTES);
            }
    }
}
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70

0 Answers0