This commit is contained in:
Looly
2022-10-24 22:47:18 +08:00
parent 106e465014
commit 267e16e9c1

View File

@@ -30,18 +30,28 @@ import java.util.ArrayList;
public class FileCopier extends SrcToDestCopier<File, FileCopier> {
private static final long serialVersionUID = 1L;
/** 是否覆盖目标文件 */
/**
* 是否覆盖目标文件
*/
private boolean isOverride;
/** 是否拷贝所有属性 */
/**
* 是否拷贝所有属性
*/
private boolean isCopyAttributes;
/** 当拷贝来源是目录时是否只拷贝目录下的内容 */
/**
* 当拷贝来源是目录时是否只拷贝目录下的内容
*/
private boolean isCopyContentIfDir;
/** 当拷贝来源是目录时是否只拷贝文件而忽略子目录 */
/**
* 当拷贝来源是目录时是否只拷贝文件而忽略子目录
*/
private boolean isOnlyCopyFile;
//-------------------------------------------------------------------------------------------------------- static method start
/**
* 新建一个文件复制器
*
* @param srcPath 源文件路径相对ClassPath路径或绝对路径
* @param destPath 目标文件路径相对ClassPath路径或绝对路径
* @return this
@@ -52,6 +62,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
/**
* 新建一个文件复制器
*
* @param src 源文件
* @param dest 目标文件
* @return this
@@ -62,8 +73,10 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
//-------------------------------------------------------------------------------------------------------- static method end
//-------------------------------------------------------------------------------------------------------- Constructor start
/**
* 构造
*
* @param src 源文件
* @param dest 目标文件
*/
@@ -74,15 +87,19 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
//-------------------------------------------------------------------------------------------------------- Constructor end
//-------------------------------------------------------------------------------------------------------- Getters and Setters start
/**
* 是否覆盖目标文件
*
* @return 是否覆盖目标文件
*/
public boolean isOverride() {
return isOverride;
}
/**
* 设置是否覆盖目标文件
*
* @param isOverride 是否覆盖目标文件
* @return this
*/
@@ -93,13 +110,16 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
/**
* 是否拷贝所有属性
*
* @return 是否拷贝所有属性
*/
public boolean isCopyAttributes() {
return isCopyAttributes;
}
/**
* 设置是否拷贝所有属性
*
* @param isCopyAttributes 是否拷贝所有属性
* @return this
*/
@@ -110,6 +130,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
/**
* 当拷贝来源是目录时是否只拷贝目录下的内容
*
* @return 当拷贝来源是目录时是否只拷贝目录下的内容
*/
public boolean isCopyContentIfDir() {
@@ -118,6 +139,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
/**
* 当拷贝来源是目录时是否只拷贝目录下的内容
*
* @param isCopyContentIfDir 是否只拷贝目录下的内容
* @return this
*/
@@ -168,7 +190,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
@Override
public File copy() throws IORuntimeException {
final File src = this.src;
final File dest = this.dest;
File dest = this.dest;
// check
Assert.notNull(src, "Source File is null !");
if (false == src.exists()) {
@@ -191,12 +213,13 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
final File subTarget = isCopyContentIfDir ? dest : FileUtil.mkdir(FileUtil.file(dest, src.getName()));
internalCopyDirContent(src, subTarget);
} else {// 复制文件
internalCopyFile(src, dest);
dest = internalCopyFile(src, dest);
}
return dest;
}
//----------------------------------------------------------------------------------------- Private method start
/**
* 拷贝目录内容,只用于内部,不做任何安全检查<br>
* 拷贝内容的意思为源目录下的所有文件和目录拷贝到另一个目录下,而不拷贝源目录本身
@@ -246,12 +269,13 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
*
* @param src 源文件,必须为文件
* @param dest 目标文件,如果非覆盖模式必须为目录
* @return 目标文件
* @throws IORuntimeException IO异常
*/
private void internalCopyFile(final File src, File dest) throws IORuntimeException {
private File internalCopyFile(final File src, File dest) throws IORuntimeException {
if (null != copyPredicate && false == copyPredicate.test(src)) {
//被过滤的文件跳过
return;
return dest;
}
// 如果已经存在目标文件,切为不覆盖模式,跳过之
@@ -263,7 +287,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
if (dest.exists() && false == isOverride) {
//非覆盖模式跳过
return;
return dest;
}
} else {
//路径不存在则创建父目录
@@ -283,6 +307,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return dest;
}
//----------------------------------------------------------------------------------------- Private method end
}