add SaveFile

This commit is contained in:
Burgess Leo
2025-05-20 18:01:19 +08:00
parent 08a47c6d8a
commit cd536a6bd3
2 changed files with 49 additions and 1 deletions

View File

@@ -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()

47
test/SaveToFile.py Normal file
View File

@@ -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"
)