一、制作
'''
说明:
Project.data6文件 存储项目数据的二进制文件
info.Bint文件 项目文件中每一个子文件的二进制数据,在Project.data6二进制文件中的起始位置信息,如果缺少该文件,则程序
无法解压Project.data6文件
'''
import sys, os,zipfile
import zipfile as zip
from PyQt5.QtWidgets import *
class myWin(QWidget):
def __init__(self, parent=None):
super(myWin, self).__init__(parent)
self.setWindowTitle('定制格式文件')
self.setUI()
def setUI(self):
lay=QHBoxLayout()
self.setLayout(lay)
bt1=QPushButton('保存定制格式文件')
bt1.clicked.connect(self.WriteFileFormat)
bt2=QPushButton('解压定制格式文件')
bt2.clicked.connect(self.ReadFileFormat)
lay.addWidget(bt1)
lay.addWidget(bt2)
def WriteFileFormat(self):
position_list= []
position_num=0
file_name_list=[]
read_path,_=QFileDialog.getOpenFileNames(self,'选择文件','','所有文件(*)')
# print(type(read_path))
# print(read_path)
for file in read_path:
# print(file)
file_name=file.split(sep='/')[-1]
# print(file_name)
file_name_list.append(file_name)
path1=file
# print(path1)
with open(path1,'rb') as f1:
data=f1.read()
lenght=len(data)
position_num=position_num+lenght
position_list.append(position_num)
# print(path1)
# print(lenght)
# print('-'*60)
with open('Project.data6','ab') as f2:
f2.write(data)
# print(position_list)
start_end_position_list=[] # 打包进文件中的文件,在定制格式文件中数据中的切片位置列表,每一个元素都是该文件的切片位置
for i in range(len(position_list)):
if i ==0:
start_end_position=[0,position_list[i]]
start_end_position_list.append(start_end_position)
else:
start_end_position = [position_list[i-1], position_list[i]]
start_end_position_list.append(start_end_position)
# print(file_name_list)
# print(start_end_position_list)
info_dict={file_name_list[i]:start_end_position_list[i] for i in range(len(file_name_list))}
with open('info.Bint','w') as info:
for key,value in info_dict.items():
info.write(f'{key}:{value}\n')
# print(info_dict)
# 添加至压缩包
save_path, _ = QFileDialog.getSaveFileName(self, '保存文件', '', '定制文件格式(*.BLH9)')
zp = zip.ZipFile(file=save_path, mode='w', compression=zipfile.ZIP_STORED,
allowZip64=False)
zp.write(filename='Project.data6', arcname='Project.data6')
zp.write(filename='info.Bint', arcname='info.Bint')
os.remove('Project.data6')
os.remove('info.Bint')
zp.close()
def ReadFileFormat(self):
path,_=QFileDialog.getOpenFileName(self,'打开BHL9文件','','BLH9文件(*.BLH9)')
zp=zip.ZipFile(file=path,mode='r')
current_path=os.path.dirname(sys.argv[0])
zp.extractall(current_path)
zp.close()
# read_path,_=QFileDialog.getOpenFileName(self,'打开info文件','','info文件(*.txt1)')
out_path=QFileDialog.getExistingDirectory(self,'导出BLH9文件','')
with open(os.path.join(current_path,'info.Bint'),'r') as f1:
info_data=f1.read()
# os.remove(os.path.join(current_path,'info.Bint'))
# print(data.split(sep='\n'))
with open(os.path.join(current_path,'Project.data6'),'rb') as f2:
project_data=f2.read()
# os.remove(os.path.join(current_path,'Project.data6'))
for info in info_data.split(sep='\n'):
if len(info) !=0:
# print(info)
file_name=info.split(sep=':')[0]
part1=info.split(sep=':')[1]
part2=part1.replace('[','')
part3=part2.replace(']','')
# print(file_name,'\n',part3,type(part3))
start_num=int(part3.split(sep=',')[0])
end_num = int(part3.split(sep=',')[1])
# print(file_name,start_num,end_num)
child_data=project_data[start_num:end_num]
with open(os.path.join(out_path,f'new_{file_name}'),'wb') as f3:
f3.write(child_data)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = myWin()
window.show()
sys.exit(app.exec())
二、使用
代码:
import win32api,win32con
import os,sys
def SetRegedit(FileSuffix,AppFile):
# 创建注册表项名称为文件后缀并设置值
key1=win32api.RegCreateKey(win32con.HKEY_CLASSES_ROOT,FileSuffix)
win32api.RegSetValueEx(key1, None, 0, win32con.REG_SZ, AppFile)
# 创建文件名称子项并设置值
key2=win32api.RegCreateKey(win32con.HKEY_CLASSES_ROOT,AppFile)
win32api.RegSetValueEx(key2,None,0,win32con.REG_SZ,'TEST小白数据')
# 设置icon
path = os.path.dirname(sys.argv[0])
ico_path=os.path.join(path,'empty.ico')
key2_icon=win32api.RegCreateKey(win32con.HKEY_CLASSES_ROOT,rf'{AppFile}\DefaultIcon')
win32api.RegSetValueEx(key2_icon,None,0,win32con.REG_SZ,ico_path)
# 设置默认打开的exe程序 todo 待研究
# key2_exe=win32api.RegCreateKey(win32con.HKEY_CLASSES_ROOT,rf'{AppFile}\Shell\Open\Command')
# path="'D:\我的文档\Desktop\制定文件阅读器\制定文件阅读器.exe'"
# win32api.RegSetValueEx(key2_exe,None,0,win32con.REG_SZ, f'{path} %1 1')
SetRegedit(FileSuffix='.BLH9',AppFile='blh9_flie')