copyFileToAsync static method
文件拷贝
path 源文件路径
pathTarget 目标文件路径(需要包括文件名称)
showLog 是否展示消息日志
targetIsDirectory 目标路径是否是文件夹
Implementation
static Future<void> copyFileToAsync(
String path,
String pathTarget, {
bool showLog = true,
bool targetIsDirectory = false,
}) async {
if (!await existsAsync(path) || !(await isFileAsync(path))) {
return;
}
// 增加处理 - 判断是否需要创建目录
String mergedPathTarget = pathTarget;
if (targetIsDirectory) {
if (!(await existsAsync(pathTarget))) {
await mkdirAsync(pathTarget, showLog: showLog);
}
PathUtil.basename(path);
final fileName = path.substring(path.lastIndexOf(PathUtil.separator) + 1);
mergedPathTarget = PathUtil.join(pathTarget, fileName);
} else {
final targetDir = PathUtil.dirname(pathTarget);
if (!(await existsAsync(targetDir))) {
await mkdirAsync(targetDir, showLog: showLog);
}
}
if (showLog) {
final samePath = TextUtil.getSameTextInTwoStr(path, mergedPathTarget);
Logger.info(
'copy',
'${path.replaceFirst(samePath, '')} -> ${mergedPathTarget.replaceFirst(samePath, '')}',
);
}
await File(path).copy(mergedPathTarget);
}