| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import 'dart:io';
- import 'dart:math';
- import 'package:convert/convert.dart';
- import 'package:crypto/crypto.dart';
- import 'package:path/path.dart' as path;
- import 'package:file_selector/file_selector.dart' as file_selector;
- import 'package:path_provider/path_provider.dart';
- 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);
- }
- Future<int> fileSizeOf(path) async {
- final file = File(path);
- final size = await file.length();
- return size;
- }
- String formatFileSize(int bytes) {
- if (bytes <= 0) return "0 B";
- const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
- var i = (log(bytes) / log(1024)).floor();
- return ((bytes / pow(1024, i)).toStringAsFixed(2)) + ' ' + suffixes[i];
- }
- // MD5
- Future<String> getFileMd5(String filePath) async {
- final fileExists = File(filePath).existsSync();
- if (!fileExists) throw FileSystemException("File does not exist", filePath);
- final file = File(filePath);
- final digest = await file.openRead().transform(md5).first;
- return hex.encode(digest.bytes);
- }
- 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);
- }
- Future<String?> getSaveLocation({required String suggestedName, String? iOSAppDirectory}) async {
- if (Platform.isIOS) {
- final docDir = await getApplicationDocumentsDirectory();
- String path = docDir.path;
- if (iOSAppDirectory != null) {
- path = '${docDir.path}/$iOSAppDirectory';
- }
- Directory dir = Directory(path);
- // 检查文件夹是否已经存在
- bool exists = await dir.exists();
- if (!exists) {
- // 如果文件夹不存在,则创建它
- await dir.create(recursive: true); // 设置 recursive 为 true 可以创建多级目录
- }
- final finalPath = File('${dir.path}/$suggestedName').path;
- // Log.d('iOS get file path: $finalPath');
- return finalPath;
- } else {
- final file_selector.FileSaveLocation? selectedFile = await file_selector.getSaveLocation(
- suggestedName: suggestedName,
- );
- if (selectedFile != null) {
- return selectedFile.path;
- }
- }
- return null;
- }
|