copyFile static method

FileResult<File> copyFile(
  1. String sourcePath,
  2. String targetPath
)

复制文件

Copies a file

Implementation

static FileResult<File> copyFile(String sourcePath, String targetPath) {
  try {
    final sourceFile = File(sourcePath);
    if (!sourceFile.existsSync()) {
      return const FileResult.failure('Source file does not exist');
    }

    final targetFile = File(targetPath);
    // 确保目标目录存在
    final targetDir = targetFile.parent;
    if (!targetDir.existsSync()) {
      targetDir.createSync(recursive: true);
    }

    final copiedFile = sourceFile.copySync(targetPath);
    return FileResult.success(copiedFile);
  } catch (e, stack) {
    loge('Failed to copy file from $sourcePath to $targetPath: $e\n$stack');
    return FileResult.failure(e.toString());
  }
}