通过二进制读取文件后,切片出最后8个字节,比对一下前四个是否是文件标识符“563412FA”,如果是,就可以转换。再取后四位,是十六进制显示的bytes,如b'\x1c\x97\x01\x00',可以使用struct模块,unpack方法换算成一个长整数。Byte Order, Size, and Alignment
By default, C types are represented in the machine’s native format and byteorder, and properly aligned by skipping pad bytes if necessary (according to therules used by the C compiler).Alternatively, the first character of the format string can be used to indicatethe byte order, size and alignment of the packed data, according to thefollowing table:[td]
Character
Byte order
Size
Alignment
@
native
native
native
=
native
standard
none
<
little-endian
standard
none
>
big-endian
standard
none
!
network (= big-endian)
standard
none
Format Characters
Format characters have the following meaning; the conversion between C andPython values should be obvious given their types. The ‘Standard size’ columnrefers to the size of the packed value in bytes when using standard size; thatis, when the format string starts with one of '<', '>', '!' or'='. When using native size, the size of the packed value isplatform-dependent.[td]
#!/usr/bin/env python
# coding: utf-8
import re,os,struct
def exe2swf(path_list):
notexeflash=[]
exe2swf_done=[]
for path in path_list:
with open(path,'rb') as f1:
f1.seek(-8,2)
if re.match(b'V4\x12\xfa',f1.read(4)):
swf_l=struct.unpack('<I', f1.read(4))[0
f1.seek(-8-swf_l,2)
with open(path+'.swf','wb') as f2:
f2.write(f1.read(swf_l))
exe2swf_done.append(path)
else:
notexeflash.append(path)
print('转换完成,返回值为不能转换的列表和已转换的列表的两个元素的元组')
return notexeflash,exe2swf_done
def list_all(path):
l=[]
def recursion(path):
if os.path.isfile(path):
if path.endswith('exe'):
l.append(path)
elif os.path.isdir(path):
tmp=[path+os.sep+x for x in
for i in tmp:
recursion(i)
return l
return recursion(path)
if __name__ == "__main__":
path=os.path.split(os.path.realpath(__file__))
lexe=list_all(path)
nots,dones=exe2swf(lexe)
if input('需要删除已完成转换的exe文件吗?输入“yes”即可删除')=='yes':
for i in dones:
os.remove(i)