Files
fastcopy/test/SaveToFile.py
Burgess Leo cd536a6bd3 add SaveFile
2025-05-20 18:01:19 +08:00

48 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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