初始化
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// ignore_for_file: constant_identifier_names
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:hgdj/extension/extensions.dart';
|
||||
import 'package:hgdj/hj_utils/text_util.dart';
|
||||
import 'package:hgdj/tools_base/debug_log.dart';
|
||||
|
||||
const KB_SIZE = 1024;
|
||||
const MB_SIZE = 1024 * KB_SIZE;
|
||||
const GB_SIZE = 1024 * MB_SIZE;
|
||||
|
||||
/// 文件相关的公共处理
|
||||
class FileUtil {
|
||||
/// 计算切片个数
|
||||
static int getPatchCount(int fileLen) {
|
||||
final cutSize = getPatchSize(fileLen);
|
||||
return (fileLen + cutSize - 1) ~/ cutSize;
|
||||
}
|
||||
|
||||
/// 根据文件长度计算切片大小
|
||||
static int getPatchSize(int fileLen) {
|
||||
if (fileLen < MB_SIZE) return MB_SIZE;
|
||||
return 2 * MB_SIZE;
|
||||
}
|
||||
|
||||
/// 文件是否存在
|
||||
static bool isFileExist(String path) {
|
||||
return TextUtil.isNotEmpty(path) && File(path).existsSync();
|
||||
}
|
||||
|
||||
/// 获取 file 从 offset 之后到 blockSize 的数据块
|
||||
/// [offset] 起始偏移位置
|
||||
/// [blockSize] 分块大小
|
||||
/// [file] 文件
|
||||
static Future<Uint8List> getFileBlock(
|
||||
int offset, int blockSize, File file) async {
|
||||
RandomAccessFile? accessFile;
|
||||
try {
|
||||
accessFile = await file.open();
|
||||
await accessFile.setPosition(offset);
|
||||
debugLog('offset:$offset blocksize:$blockSize');
|
||||
return await accessFile.read(blockSize);
|
||||
} on Exception {
|
||||
return Uint8List(0);
|
||||
} finally {
|
||||
accessFile?.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取文件长度
|
||||
static int getFileSize(String path) {
|
||||
if (!isFileExist(path)) return 0;
|
||||
return File(path).lengthSync();
|
||||
}
|
||||
|
||||
/// 获取文件的格式化大小
|
||||
static String byteFmt(int size) {
|
||||
if (size > GB_SIZE) {
|
||||
return '${(size / GB_SIZE).toStringAsFixed(1)}GB';
|
||||
} else if (size > MB_SIZE) {
|
||||
return '${(size / MB_SIZE).toStringAsFixed(1)}MB';
|
||||
} else {
|
||||
return '${(size / KB_SIZE).toStringAsFixed(1)}KB';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取文件名带后缀
|
||||
/// 支持 url/uri/file/abspath
|
||||
static String getName(String absPath) {
|
||||
if (TextUtil.isEmpty(absPath)) return absPath;
|
||||
absPath = Uri.parse(absPath).path;
|
||||
final start = absPath.lastIndexOf('/');
|
||||
if (start <= 0 || start == absPath.length - 1) {
|
||||
return absPath;
|
||||
}
|
||||
return absPath.substring(start + 1);
|
||||
}
|
||||
|
||||
/// 获取文件名不带后缀
|
||||
static String getNamePrefix(String absPath) {
|
||||
final name = getName(absPath);
|
||||
if (TextUtil.isEmpty(name)) return name;
|
||||
final ar = name.split('.');
|
||||
if (ar.empty()) return name;
|
||||
return ar[0];
|
||||
}
|
||||
|
||||
/// 获取文件名后缀 (fileExtension)
|
||||
static String getNameSuffix(String absPath) {
|
||||
final name = getName(absPath);
|
||||
if (TextUtil.isEmpty(name)) return name;
|
||||
final ar = name.split('.');
|
||||
if (ar.empty()) return name;
|
||||
if (ar.length < 2) return ar[0];
|
||||
return ar.last;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user