gpt4 book ai didi

java-7 - 如何将具有属性/权限的目录从一个位置复制到另一个位置?

转载 作者:行者123 更新时间:2023-12-01 15:48:26 24 4
gpt4 key购买 nike

我看到很多examples使用 Files.walkFileTree()将目录及其内容从一个位置复制到另一个位置,但它们没有考虑目录的文件属性和权限。意思是,他们只是调用 Files.createDirectories()没有任何属性或权限。

如何使用 Java7 核心类将目录(及其内容)从一个位置复制到另一个位置而不丢失文件属性或权限?

最佳答案

回答我自己的问题:

/**
* Copies a directory.
* <p>
* NOTE: This method is not thread-safe.
* <p>
*
* @param source
* the directory to copy from
* @param target
* the directory to copy into
* @throws IOException
* if an I/O error occurs
*/
private static void copyDirectory(final Path source, final Path target)
throws IOException {
Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE, new FileVisitor<Path>() {

@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes sourceBasic) throws IOException {
Path targetDir = Files.createDirectories(target
.resolve(source.relativize(dir)));
AclFileAttributeView acl = Files.getFileAttributeView(dir,
AclFileAttributeView.class);
if (acl != null)
Files.getFileAttributeView(targetDir,
AclFileAttributeView.class).setAcl(acl.getAcl());
DosFileAttributeView dosAttrs = Files.getFileAttributeView(
dir, DosFileAttributeView.class);
if (dosAttrs != null) {
DosFileAttributes sourceDosAttrs = dosAttrs
.readAttributes();
DosFileAttributeView targetDosAttrs = Files
.getFileAttributeView(targetDir,
DosFileAttributeView.class);
targetDosAttrs.setArchive(sourceDosAttrs.isArchive());
targetDosAttrs.setHidden(sourceDosAttrs.isHidden());
targetDosAttrs.setReadOnly(sourceDosAttrs.isReadOnly());
targetDosAttrs.setSystem(sourceDosAttrs.isSystem());
}
FileOwnerAttributeView ownerAttrs = Files
.getFileAttributeView(dir, FileOwnerAttributeView.class);
if (ownerAttrs != null) {
FileOwnerAttributeView targetOwner = Files
.getFileAttributeView(targetDir,
FileOwnerAttributeView.class);
targetOwner.setOwner(ownerAttrs.getOwner());
}
PosixFileAttributeView posixAttrs = Files
.getFileAttributeView(dir, PosixFileAttributeView.class);
if (posixAttrs != null) {
PosixFileAttributes sourcePosix = posixAttrs
.readAttributes();
PosixFileAttributeView targetPosix = Files
.getFileAttributeView(targetDir,
PosixFileAttributeView.class);
targetPosix.setPermissions(sourcePosix.permissions());
targetPosix.setGroup(sourcePosix.group());
}
UserDefinedFileAttributeView userAttrs = Files
.getFileAttributeView(dir,
UserDefinedFileAttributeView.class);
if (userAttrs != null) {
UserDefinedFileAttributeView targetUser = Files
.getFileAttributeView(targetDir,
UserDefinedFileAttributeView.class);
for (String key : userAttrs.list()) {
ByteBuffer buffer = ByteBuffer.allocate(userAttrs
.size(key));
userAttrs.read(key, buffer);
buffer.flip();
targetUser.write(key, buffer);
}
}
// Must be done last, otherwise last-modified time may be
// wrong
BasicFileAttributeView targetBasic = Files
.getFileAttributeView(targetDir,
BasicFileAttributeView.class);
targetBasic.setTimes(sourceBasic.lastModifiedTime(),
sourceBasic.lastAccessTime(),
sourceBasic.creationTime());
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
Files.copy(file, target.resolve(source.relativize(file)),
StandardCopyOption.COPY_ATTRIBUTES);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult
visitFileFailed(Path file, IOException e)
throws IOException {
throw e;
}

@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException e) throws IOException {
if (e != null) throw e;
return FileVisitResult.CONTINUE;
}
});
}

此代码尚未经过广泛测试,但对我有用。

关于java-7 - 如何将具有属性/权限的目录从一个位置复制到另一个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com