diff --git a/src/utils/helper_functions.py b/src/utils/helper_functions.py index af3cb2f..db80af9 100644 --- a/src/utils/helper_functions.py +++ b/src/utils/helper_functions.py @@ -11,8 +11,21 @@ class Utils: - 尝试多种编码格式 - 返回最可能的编码 """ - # TODO: 实现编码检测逻辑 - pass + import chardet + + # 读取文件的前1024字节用于编码检测 + with open(file_path, 'rb') as f: + raw_data = f.read(1024) + + # 使用chardet检测编码 + result = chardet.detect(raw_data) + encoding = result['encoding'] + + # 如果chardet无法确定编码,则默认使用utf-8 + if encoding is None: + encoding = 'utf-8' + + return encoding @staticmethod def format_file_size(size_bytes: int) -> str: @@ -20,9 +33,25 @@ class Utils: 格式化文件大小 - 将字节数转换为可读格式 - 返回格式化字符串 + 参数: + size_bytes (int): 需要格式化的文件大小,单位为字节 + 返回: + str: 格式化后的文件大小字符串,如 "1.5 MB" """ - # TODO: 实现文件大小格式化逻辑 - pass + # 如果文件大小为0字节,直接返回 "0 B" + if size_bytes == 0: + return "0 B" + + # 定义文件大小单位列表 + size_names = ["B", "KB", "MB", "GB", "TB"] + i = 0 + # 当文件大小大于等于1024且未到达最大单位时,循环除以1024 + while size_bytes >= 1024.0 and i < len(size_names) - 1: + size_bytes /= 1024.0 + i += 1 + + # 返回格式化后的字符串,保留一位小数 + return f"{size_bytes:.1f} {size_names[i]}" @staticmethod def calculate_file_hash(file_path: str) -> str: @@ -31,5 +60,11 @@ class Utils: - 使用SHA256算法 - 返回哈希字符串 """ - # TODO: 实现文件哈希计算逻辑 - pass \ No newline at end of file + sha256_hash = hashlib.sha256() + + # 分块读取文件以避免大文件占用过多内存 + with open(file_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + sha256_hash.update(chunk) + + return sha256_hash.hexdigest() \ No newline at end of file diff --git a/src/utils/test_helper_functions.py b/src/utils/test_helper_functions.py new file mode 100644 index 0000000..e73ec07 --- /dev/null +++ b/src/utils/test_helper_functions.py @@ -0,0 +1,29 @@ +# test_helper_functions.py +import os +from helper_functions import Utils + +# 创建一个测试文件 +test_content = "这是一个测试文件,用于验证工具函数。\nThis is a test file to verify utility functions." +test_file_path = "test_file.txt" + +# 写入测试文件 +with open(test_file_path, "w", encoding="utf-8") as f: + f.write(test_content) + +# 测试文件大小格式化 +file_size = os.path.getsize(test_file_path) +formatted_size = Utils.format_file_size(file_size) +print(f"文件大小: {file_size} 字节") +print(f"格式化大小: {formatted_size}") + +# 测试文件编码检测 +detected_encoding = Utils.detect_encoding(test_file_path) +print(f"检测到的编码: {detected_encoding}") + +# 测试文件哈希计算 +file_hash = Utils.calculate_file_hash(test_file_path) +print(f"文件哈希值: {file_hash}") + +# 清理测试文件 +os.remove(test_file_path) +print("测试完成,临时文件已清理。") \ No newline at end of file