开发者

基于Python自制一个文件解压缩小工具

经常在办公的过程中会遇到各种各样的压缩文件处理,但是呢每个压缩软件支持的格式又是不同的。

没有可以一种可以同时多种格式的并且免费的文件解压缩工具,于是我使用python的PyQt5开发出这个文件解压缩的小工具。

接下来,我们将开发过程中需要的python非标准库以及代码块做一个简单的介绍,有兴趣的小伙伴可以停下脚步一起来看看。

一般在Windows的操作系统下文件解压缩的格式就是7z/zip/rar这三种,首先我们需要安装一下PyQt5以及需要文件解压缩处理的模块。

这里我们直接使用的是pip的安装方式进行安装,我的pip默认配置的是全局的清华大学镜像站。

pipinstallPyQt5
pipinstallpy7zr
pipinstallrarfile

然后,在开始之前我们将需要的python标准或非标准模块全部导入代码块中准备进入下面的开发环节。

#ImportingalltheclassesfromthePyQt5.QtGuimodule.
fromPyQt5.QtGuiimport*

#ImportingalltheclassesfromthePyQt5.QtWidgetsmodule.
fromPyQt5.QtWidgetsimport*

#ImportingalltheclassesfromthePyQt5.QtCoremodule.
fromPyQt5.QtCoreimport*

#`importos`isimportingtheosmodule.
importos

#`importsys`isimportingthesysmodule.
importsys

#`importzipfileaszip`isimportingthezipfilemoduleaszip.
importzipfileaszip

#`importpy7zr`isimportingthepy7zrmodule.
importpy7zr

#`importrarfileasrar`isimportingtherarfilemoduleasrar.
importrarfileasrar

#Importingthetracebackmodule.
importtraceback

importimages

至此,我们开发需要使用到的python模块就全部导入进来了,这里说明一下我们使用到的英文注释是通过pycharm的AI插件直接生成的。

首先,创建一个名称为CompressUI的python类,将所有的UI页面组件及布局全部放在这个类中进行开发。

以及包括UI页面组件关联的槽函数也放在这个类中,也就是在Comp编程客栈ressUI类中我们只处理页面操作相关的部分不做具体逻辑的实现。

classCompressUI(QWidget):
def__init__(self):
super(CompressUI,self).__init__()
self.init_ui()

definit_ui(self):
self.setWindowTitle('文件解压缩处理工具公众号:Python 集中营')
self.setWindowIcon(QIcon(':/analysis.ico'))
self.resize(600,400)

self.compress_file_type=QLabel()
self.compress_file_type.setText('解压缩文件类型:')

self.compress_file_type_combox=QComboBox()
self.compress_file_type_combox.addItems(['7z格式','zip格式','rar格式'])

self.file_catch_type=QLabel()
self.file_catch_type.setText('文件处理方式:')

self.file_catch_type_combox=QComboBox()
self.file_catch_type_combox.addItems(['压缩','解压缩'])

self.source_dir_or_file=QLineEdit()
self.source_dir_or_file.setPlaceholderText('来源目录或文件路径...')

self.source_dir_or_file_btn=QPushButton()
self.source_dir_or_file_btn.setText('加载来源目录或文件')
self.source_dir_or_file_btn.clicked.connect(self.source_dir_or_file_btn_clk)

self.target_dir_or_file=QLineEdit()
self.target_dir_or_file.setPlaceholderText('目标目录路径...')

self.target_dir_or_file_btn=QPushButton()
self.target_dir_or_file_btn.setText('选择目标路径')
self.target_dir_or_file_btn.clicked.connect(self.target_dir_or_file_btn_clk)

self.start_btn=QPushButton()
self.start_btn.setText('开始执行文件压缩或解压缩处理')
self.start_btn.clicked.connect(self.start_btn_clk)

self.brower=QTextBrowser()
self.brower.setReadOnly(True)
self.brower.setFont(QFont('宋体',8))
self.brower.setPlaceholderText('日志处理过程区域...')
self.brower.ensureCursorVisible()

grid=QGridLayout()
grid.addwidget(self.compress_file_type,0,0,1,2)
grid.addWidget(self.compress_file_type_combox,0,2,1,1)
grid.addWidget(self.file_catch_type,1,0,1,2)
grid.addWidget(self.file_catch_type_combox,1,2,1,1)
grid.addWidget(self.source_dir_or_file,2,0,1,2)
grid.addWidget(self.source_dir_or_file_btn,2,2,1,1)
grid.addWidget(self.target_dir_or_file,3,0,1,2)
grid.addWidget(self.target_dir_or_file_btn,3,2,1,1)
grid.addWidget(self.start_btn,4,0,1,3)
grid.addWidget(self.brower,5,0,1,3)

self.thread_=WorkThread(self)
self.thread_.message.connect(self.show_message)
self.thread_.finished.connect(self.thread_is_finished)

self.setLayout(grid)

defshow_message(self,text):
cursor=self.brower.textCursor()
cursor.movePosition(QTextCursor.End)
self.brower.append(text)
self.brower.setTextCursor(cursor)
self.brower.ensureCursorVisible()

deftarget_dir_or_file_btn_clk(self):
target_dir_or_file_path=QFileDialog.getExistingDirectory(self,'选择文件夹',os.getcwd())
self.target_dir_or_file.setText(target_dir_or_file_path)

defsource_dir_or_file_btn_clk(self):
file_catch_type=self.file_catch_type_combox.currentText()
iffile_catch_type=='压缩':
source_dir_or_file_path=QFileDialog.getExistingDirectory(self,'选择文件夹',os.getcwd())
self.source_dir_or_file.setText(source_dir_or_file_path)
else:
source_dir_or_file_path=QFileDialog.getOpenFileName(self,"选取文件",os.getcwd(),
"RARFile(*.rar);;ZIPFile(*.zip);;7zFile(*.7z)")
self.source_dir_or_file.setText(source_dir_or_file_path[0])

defstart_btn_clk(self):
self.start_btn.setEnabled(False)
self.thread_.start()

defthread_is_finished(self,text):
iftextisTrue:
self.start_btn.setEnabled(True)

以上就是整个UI页面组件/布局以及组件对应的槽函数的的开发过程了,有需要的小伙伴可以仔细研究一下。

基于Python自制一个文件解压缩小工具

接下来进入具体业务的开发环节,我们创建一个名称为WorkThread的python类,该类继承自QThread的子线程。

并且在子线程中可以接收主线程的变量参数,以及向主线程中传递信息的操作。将子线程执行的过程信息实时传递到主线程的文本浏览器中。

classWorkThread(QThread):
message=pyqtSignal(str)
finished=pyqtSignal(bool)

def__init__(self,parent=None):
super(WorkThread,self).__init__(parent)
self.parent=parent
self.working=True

def__del__(self):
self.working=False

defrun(self):
try:
compress_file_type=self.parent.comp开发者_开发教程ress_file_type_combox.currentText()
file_catch_type=self.parent.file_catch_type_combox.currentText()
source_dir_or_file=self.parent.source_dir_or_file.text().strip()
target_dir_or_file=self.parent.target_dir_or_file.text().strip()
ifsource_dir_or_file==''ortarget_dir_or_file=='':
self.message.emit('来源或目标文件路径为空,请检查参数设置!')
return
iffile_catch_type=='压缩'anDOS.path.isfile(source_dir_or_file):
self.message.emit('当处理类型为:压缩,来源类型应该选择文件夹,请按顺序设置参数!')
return
iffile_catch_type=='解压缩'andos.path.isdir(source_dir_or_file):
self.message.emit('当处理类型为:解压缩,来源类型应该选择文件,请按顺序设置参数!')
return
self.message.emit('准备处理的格式类星星为:{}'.format(compress_file_type))
self.message.emit('准备处理的处理类型为:{}'.format(file_catch_type))
self.message.emit('来源文件或目录的路径为:{}'.format(source_dir_or_file))
self.message.emit('目标目录的路径为:{}'.format(target_dir_or_file))

ifcompress_file_type=='zip格式':
iffile_catch_type=='压缩':
self.do_zip(source_dir_or_file,targetandroid_dir_or_file)
else:
self.un_zip(source_dir_or_file,target_dir_or_file)
elifcompress_file_type=='rar格式':
iffile_catch_type=='压缩':
self.message.emit('rar格式的文件压缩正在玩命开发中,请关注后续版本更新!')
else:
self.un_rar(source_dir_or_file,target_dir_or_file)
elifcompress_file_type=='7z格式':
iffile_catch_type=='压缩':
self.do_7z(source_dir_or_file,target_dir_or_file)
else:
self.un_7z(androidsource_dir_or_file,target_dir_or_file)
self.message.emit('当前处理过程:{}完成!'.format(file_catch_type))
self.finished.emit(True)
except:
traceback.print_exc()
self.finished.emit(True)

defdo_zip(self,source_,target_file):
"""
Iftheuserselectsthe"压缩"option,thentheusercanselectadirectory,andthepathofthedirectorywillbe
displayedinthetextbox
"""
zip_file=zip.ZipFile(target_file,'w')
pre_len=len(os.path.dirname(source_))
forparent,dirnames,filenamesinos.walk(source_):
forfilenameinfilenames:
print(f'{filename}')
path_file=os.path.join(parent,filename)
arcname=path_file[pre_len:].strip(os.path.sep)
zip_file.write(path_file,arcname)

zip_file.close()

defun_zip(self,source_file,target_):
"""
>Unzipafiletoatargetdirectory

:paramsource_file:Thefileyouwanttounzip
:paramtarget_:thedirectorywhereyouwanttounzipthefile
"""
zip_file=zip.ZipFile(source_file)
ifos.path.isdir(target_):
pass
else:
os.mkdir(target_)
fornamesinzip_file.namelist():
zip_file.extract(names,target_)
zip_file.close()

defdo_7z(self,source_,target_file):
"""
>Thisfunctiontakesasourcefileandatargetfileandcompressesthesourcefileintothetargetfileusing7z

:paramsource_:Thesourcefileordirectorytobecompressed
:paramtarget_file:Thenameofthefiletobecreated
"""
withpy7zr.SevenZipFile(target_www.devze.comfile,'r')asfile:
file.extractall(path=source_)

defun_7z(self,source_file,target_):
"""
Ittakesasourcedirectoryandatargetfile,andcreatesazipfilecontainingthecontentsofthesource
directory

:paramsource_:Thepathtothefolderyouwanttozip
:paramtarget_file:Thepathtothezipfileyouwanttocreate
"""
withpy7zr.SevenZipFile(source_file,'w')a编程客栈sfile:
file.writeall(target_)

defun_rar(self,source_file,target_):
"""
Ittakesasourcefileandatargetdirectoryandunzipsthesourcefileintothetargetdirectory

:paramsource_file:ThepathtotheRARfileyouwanttoextract
:paramtarget_:Thedirectorywhereyouwantthefilestobeextractedto
"""
obj_=rar.RarFile(source_file.decode('utf-8'))
obj_.extractall(target_.decode('utf-8'))

最后,使用python模块的主函数main,将整个应用加入到主体循环过程中就可以启动整个桌面应用了。

if__name__=='__main__':
app=QApplication(sys.argv)
main=CompressUI()
main.show()
sys.exit(app.exec_())

基于Python自制一个文件解压缩小工具

完成上述的开发工作之后,我们可以选择使用常用的pyinstaller打包模块对整个应用进行打包操作,打包细节可以参考我的历史文章中的说明!

到此这篇关于基于Python自制一个文件解压缩小工具的文章就介绍到这了,更多相关Python文件解压缩工具内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜