From cd536a6bd3f358aaf011ba353edc8fffae952bfc Mon Sep 17 00:00:00 2001 From: Burgess Leo <1799594843@qq.com> Date: Tue, 20 May 2025 18:01:19 +0800 Subject: [PATCH] add SaveFile --- ntfs_utils/__init__.py | 3 ++- test/SaveToFile.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/SaveToFile.py diff --git a/ntfs_utils/__init__.py b/ntfs_utils/__init__.py index 612e6a1..3938606 100644 --- a/ntfs_utils/__init__.py +++ b/ntfs_utils/__init__.py @@ -8,7 +8,7 @@ from db_node import InsertNodeDataToDB def main(): - volume_letter = 'Z' + volume_letter = 'Y' # 初始化 db_config 表 config_data = GetNTFSBootInfo(volume_letter) @@ -41,6 +41,7 @@ def main(): count = InsertExtensionsToDB(common_extensions) print(f"共插入 {count} 个新扩展名。") + # 初始化 db_node 表 InsertNodeDataToDB() diff --git a/test/SaveToFile.py b/test/SaveToFile.py new file mode 100644 index 0000000..8497075 --- /dev/null +++ b/test/SaveToFile.py @@ -0,0 +1,47 @@ +def copy_file_from_bytes(start_byte, end_byte, source_disk_path, target_file_path): + """ + 根据起始字节和结束字节偏移量,从磁盘中读取指定范围的数据并保存为目标文件 + + 参数: + start_byte (int): 起始字节偏移量(包含) + end_byte (int): 结束字节偏移量(包含) + source_disk_path (str): 源磁盘路径(如 r"\\.\Z:") + target_file_path (str): 目标文件路径(如 r"E:\demo.jpg") + """ + if start_byte > end_byte: + print("错误:起始字节偏移量不能大于结束字节偏移量") + return + + try: + with open(source_disk_path, 'rb') as disk: + # 计算总字节数 + total_bytes = end_byte - start_byte + 1 + + # 定位到起始位置 + disk.seek(start_byte) + + # 读取指定范围内的数据 + file_data = disk.read(total_bytes) + + if not file_data or len(file_data) < total_bytes: + print(f"警告:只读取到 {len(file_data)} 字节,未达到预期 {total_bytes} 字节") + + # 写入目标文件 + with open(target_file_path, 'wb') as f: + f.write(file_data) + + print( + f"成功:已从字节偏移量 {start_byte} 到 {end_byte} 读取 {len(file_data)} 字节,保存为 {target_file_path}") + + except PermissionError: + print("错误:需要管理员权限访问磁盘设备,请以管理员身份运行此程序") + except Exception as e: + print(f"发生错误: {str(e)}") + + +copy_file_from_bytes( + start_byte=687685632, + end_byte=687685632+7163904, + source_disk_path=r"\\.\Y:", + target_file_path=r"Z:\demo.mp3" +)