|
@@ -4,113 +4,62 @@
|
|
|
import os
|
|
import os
|
|
|
import shutil
|
|
import shutil
|
|
|
import argparse
|
|
import argparse
|
|
|
-import struct
|
|
|
|
|
import binascii
|
|
import binascii
|
|
|
|
|
|
|
|
-def intToBytes(value, length):
|
|
|
|
|
- result = []
|
|
|
|
|
- for i in range(0, length):
|
|
|
|
|
- result.append(value >> (i * 8) & 0xff)
|
|
|
|
|
- result.reverse()
|
|
|
|
|
- return result
|
|
|
|
|
-
|
|
|
|
|
|
|
+def calChecknum(type="sum32", datas=[]):
|
|
|
|
|
+ if type == "sum32":
|
|
|
|
|
+ return sum(datas)
|
|
|
|
|
+ elif type == "crc32":
|
|
|
|
|
+ return binascii.crc32(datas)
|
|
|
|
|
+ else:
|
|
|
|
|
+ print("unKown type: "+type)
|
|
|
|
|
+ return datas
|
|
|
|
|
+
|
|
|
#查看文件后的16进制数据
|
|
#查看文件后的16进制数据
|
|
|
def ReadBinFile(in_file, type="sum32", en='little', block_size=1024):
|
|
def ReadBinFile(in_file, type="sum32", en='little', block_size=1024):
|
|
|
filename, file_type = os.path.splitext(in_file)
|
|
filename, file_type = os.path.splitext(in_file)
|
|
|
- out_name = None
|
|
|
|
|
checknum_bytes = None
|
|
checknum_bytes = None
|
|
|
|
|
|
|
|
- # 方式一:累加和
|
|
|
|
|
- if type == "sum32":
|
|
|
|
|
- out_name0 = filename+file_type+".sum32"
|
|
|
|
|
- f = open(in_file, "rb")
|
|
|
|
|
- o = open(out_name0, "wb+")
|
|
|
|
|
- if f and o: #二进制打开文件
|
|
|
|
|
- checknum_all = 0
|
|
|
|
|
- if block_size:
|
|
|
|
|
- while True:
|
|
|
|
|
- # 读一块
|
|
|
|
|
- datas = f.read(block_size)
|
|
|
|
|
- if len(datas) == 0:
|
|
|
|
|
- break
|
|
|
|
|
- # 这一块的checknum
|
|
|
|
|
- checknum = 0
|
|
|
|
|
- for j in range(len(datas)):
|
|
|
|
|
- checknum += datas[j]
|
|
|
|
|
- checknum_all += datas[j] # 总文件的checknum
|
|
|
|
|
- checknum_bytes = checknum.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
- # 写入数据和checknum
|
|
|
|
|
- o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- o.write(datas)
|
|
|
|
|
- o.write(bytes(checknum_bytes))
|
|
|
|
|
-
|
|
|
|
|
- else:
|
|
|
|
|
- while True:
|
|
|
|
|
- datas = f.read(1)
|
|
|
|
|
- if len(datas) == 0:
|
|
|
|
|
- break
|
|
|
|
|
- checknum_all += datas[0] # 总文件的checknum
|
|
|
|
|
- o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- o.write(datas)
|
|
|
|
|
-
|
|
|
|
|
- f.close()
|
|
|
|
|
- o.close()
|
|
|
|
|
-
|
|
|
|
|
- checknum_all_bytes = checknum_all.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
- checknum_all_str = checknum_all_bytes.hex()
|
|
|
|
|
- print(in_file, os.path.getsize(in_file), "checknum", checknum_all_str)
|
|
|
|
|
- # 输出文件名
|
|
|
|
|
- out_name1 = filename+file_type+".sum32["+checknum_all_str+"]"
|
|
|
|
|
- shutil.move(out_name0, out_name1)
|
|
|
|
|
- # 追加到文件最后
|
|
|
|
|
- with open(out_name1, "rb+") as f:
|
|
|
|
|
- f.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- f.write(bytes(checknum_all_bytes))
|
|
|
|
|
- f.close()
|
|
|
|
|
-
|
|
|
|
|
- elif type == "crc32":
|
|
|
|
|
- # 方式2:crc32
|
|
|
|
|
- out_name0 = filename+file_type+".crc32"
|
|
|
|
|
- f = open(in_file, "rb")
|
|
|
|
|
- o = open(out_name0, "wb+")
|
|
|
|
|
- if f and o: #二进制打开文件
|
|
|
|
|
- checknum_all = 0
|
|
|
|
|
- if block_size:
|
|
|
|
|
- while True:
|
|
|
|
|
- # 读一块
|
|
|
|
|
- datas = f.read(block_size)
|
|
|
|
|
- if len(datas) == 0:
|
|
|
|
|
- break
|
|
|
|
|
- # 这一块的checknum
|
|
|
|
|
- checknum = binascii.crc32(datas)
|
|
|
|
|
- checknum_bytes = checknum.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
- # 写入数据和checknum
|
|
|
|
|
- o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- o.write(datas)
|
|
|
|
|
- o.write(bytes(checknum_bytes))
|
|
|
|
|
- else:
|
|
|
|
|
- datas = f.read()
|
|
|
|
|
|
|
+ out_name0 = filename+file_type+"."+type
|
|
|
|
|
+ f = open(in_file, "rb")
|
|
|
|
|
+ o = open(out_name0, "wb+")
|
|
|
|
|
+ if f and o: #二进制打开文件
|
|
|
|
|
+ # 1.计算和写入分片校验码
|
|
|
|
|
+ if block_size:
|
|
|
|
|
+ while True:
|
|
|
|
|
+ # 读一块
|
|
|
|
|
+ datas = f.read(block_size)
|
|
|
|
|
+ if len(datas) == 0:
|
|
|
|
|
+ break
|
|
|
|
|
+ # 这一块的checknum
|
|
|
|
|
+ checknum = calChecknum(type, datas)
|
|
|
|
|
+ checknum_bytes = checknum.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
+ # 写入数据和checknum
|
|
|
o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
o.write(datas)
|
|
o.write(datas)
|
|
|
|
|
+ o.write(bytes(checknum_bytes))
|
|
|
|
|
|
|
|
- f.seek(0,0)
|
|
|
|
|
- datas = f.read()
|
|
|
|
|
- checknum_all = binascii.crc32(datas)
|
|
|
|
|
- f.close()
|
|
|
|
|
- o.close()
|
|
|
|
|
|
|
+ else:
|
|
|
|
|
+ shutil.copy(in_file, out_name0)
|
|
|
|
|
|
|
|
- checknum_all_bytes = checknum_all.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
- checknum_all_str = checknum_all_bytes.hex()
|
|
|
|
|
- print(in_file, os.path.getsize(in_file), "checknum", checknum_all_str)
|
|
|
|
|
- # 输出文件名
|
|
|
|
|
- out_name1 = filename+file_type+".crc32["+checknum_all_str+"]"
|
|
|
|
|
- shutil.move(out_name0, out_name1)
|
|
|
|
|
- # 追加到文件最后
|
|
|
|
|
- with open(out_name1, "rb+") as f:
|
|
|
|
|
- f.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
- f.write(bytes(checknum_all_bytes))
|
|
|
|
|
- f.close()
|
|
|
|
|
|
|
+ # 2.计算原始文件校验码
|
|
|
|
|
+ f.seek(0,0)
|
|
|
|
|
+ datas = f.read()
|
|
|
|
|
+ checknum_all = calChecknum(type, datas)
|
|
|
|
|
+ f.close()
|
|
|
|
|
+ o.close()
|
|
|
|
|
|
|
|
|
|
+ # 3.写入原始文件校验码
|
|
|
|
|
+ checknum_all_bytes = checknum_all.to_bytes(4, en) #"little", "big" # 大端序
|
|
|
|
|
+ print(in_file, os.path.getsize(in_file), type, checknum_all_bytes.hex())
|
|
|
|
|
+ # 输出文件名
|
|
|
|
|
+ out_name1 = filename+file_type+"."+type+"["+checknum_all_bytes.hex()+"]"
|
|
|
|
|
+ shutil.move(out_name0, out_name1)
|
|
|
|
|
+ # 追加到文件最后
|
|
|
|
|
+ with open(out_name1, "rb+") as f:
|
|
|
|
|
+ f.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
|
|
|
|
|
+ f.write(bytes(checknum_all_bytes))
|
|
|
|
|
+ f.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|