|
@@ -0,0 +1,82 @@
|
|
|
|
|
+
|
|
|
|
|
+import 'dart:io';
|
|
|
|
|
+import 'package:path/path.dart' as path;
|
|
|
|
|
+import 'logger.dart';
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+String fileOrDirectoryName(FileSystemEntity directory) {
|
|
|
|
|
+ return lastNameOfPath(directory.path);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+String lastNameOfPath(path) {
|
|
|
|
|
+ final paths = path.split(Platform.pathSeparator);
|
|
|
|
|
+ return paths.last;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool isDirectory(path) {
|
|
|
|
|
+ return FileSystemEntity.isDirectorySync(path);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Future<bool> isDirectoryExists(path) {
|
|
|
|
|
+ return Directory(path).exists();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Future<bool> isFileExists(path) {
|
|
|
|
|
+ return File(path).exists();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool isFile(path) {
|
|
|
|
|
+ return FileSystemEntity.isFileSync(path);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+createFolder(String folderPath) async {
|
|
|
|
|
+ // 创建文件夹
|
|
|
|
|
+ final Directory folder = Directory(folderPath);
|
|
|
|
|
+ // 如果文件夹不存在,则创建
|
|
|
|
|
+ if (!await folder.exists()) {
|
|
|
|
|
+ await folder.create();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Log.d("Folder already exists");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+copyDirectory(Directory source, Directory destination) async {
|
|
|
|
|
+ await for (var entity in source.list(recursive: false)) {
|
|
|
|
|
+ if (entity is Directory) {
|
|
|
|
|
+ var newDirectory = Directory(
|
|
|
|
|
+ path.join(destination.absolute.path, path.basename(entity.path)));
|
|
|
|
|
+ await newDirectory.create();
|
|
|
|
|
+ await copyDirectory(entity.absolute, newDirectory);
|
|
|
|
|
+ } else if (entity is File) {
|
|
|
|
|
+ await entity.copy(
|
|
|
|
|
+ path.join(destination.path, path.basename(entity.path)));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+clearDirectory(Directory directory) async {
|
|
|
|
|
+ // 列出所有文件和文件夹
|
|
|
|
|
+ var files = directory.listSync();
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历文件和文件夹
|
|
|
|
|
+ for (var fileEntity in files) {
|
|
|
|
|
+ if (fileEntity is File) {
|
|
|
|
|
+ // 文件直接删除
|
|
|
|
|
+ await fileEntity.delete();
|
|
|
|
|
+ } else if (fileEntity is Directory) {
|
|
|
|
|
+ // 文件夹则递归删除
|
|
|
|
|
+ await clearDirectory(fileEntity);
|
|
|
|
|
+ await fileEntity.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+deleteDirectory(String path) async {
|
|
|
|
|
+ final directory = Directory(path);
|
|
|
|
|
+ await directory.delete(recursive: true);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|