使用带有Files.copy
的Java nio StandardCopyOption.COPY_ATTRIBUTES
方法时,Zip文件系统不会复制文件属性。这应该是吗?
以下完整工作示例代码演示了此问题。它将两个文件复制到一个zip文件中:一个是普通文件,另一个是只读文件。如果你然后列出了zip文件(例如使用7-zip),你会发现它们都是正常的,而不是只读的。
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);
}
}
}