脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - 基于Python自制一个文件解压缩小工具

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

2023-06-06 14:21Sir 老王 Python

经常在办公的过程中会遇到各种各样的压缩文件处理,但是呢每个压缩软件支持的格式又是不同的。本文就来用Python自制一个文件解压缩小工具,可以支持7z/zip/rar三种格式,希望对大家有所帮助

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

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

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

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

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

?
1
2
3
pip install PyQt5
pip install py7zr
pip install rarfile

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Importing all the classes from the PyQt5.QtGui module.
from PyQt5.QtGui import *
 
# Importing all the classes from the PyQt5.QtWidgets module.
from PyQt5.QtWidgets import *
 
# Importing all the classes from the PyQt5.QtCore module.
from PyQt5.QtCore import *
 
# `import os` is importing the os module.
import os
 
# `import sys` is importing the sys module.
import sys
 
# `import zipfile as zip` is importing the zipfile module as zip.
import zipfile as zip
 
# `import py7zr` is importing the py7zr module.
import py7zr
 
# `import rarfile as rar` is importing the rarfile module as rar.
import rarfile as rar
 
# Importing the traceback module.
import traceback
 
import images

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class CompressUI(QWidget):
    def __init__(self):
        super(CompressUI, self).__init__()
        self.init_ui()
 
    def init_ui(self):
        self.setWindowTitle('文件解压缩处理工具 公众号:Python 集中营')
        self.setWindowIcon(QIcon(':/analysis.ico'))
        self.resize(600400)
 
        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, 0012)
        grid.addWidget(self.compress_file_type_combox, 0211)
        grid.addWidget(self.file_catch_type, 1012)
        grid.addWidget(self.file_catch_type_combox, 1211)
        grid.addWidget(self.source_dir_or_file, 2012)
        grid.addWidget(self.source_dir_or_file_btn, 2211)
        grid.addWidget(self.target_dir_or_file, 3012)
        grid.addWidget(self.target_dir_or_file_btn, 3211)
        grid.addWidget(self.start_btn, 4013)
        grid.addWidget(self.brower, 5013)
 
        self.thread_ = WorkThread(self)
        self.thread_.message.connect(self.show_message)
        self.thread_.finished.connect(self.thread_is_finished)
 
        self.setLayout(grid)
 
    def show_message(self, text):
        cursor = self.brower.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.brower.append(text)
        self.brower.setTextCursor(cursor)
        self.brower.ensureCursorVisible()
 
    def target_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)
 
    def source_dir_or_file_btn_clk(self):
        file_catch_type = self.file_catch_type_combox.currentText()
        if file_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(),
                                                                  "RAR File (*.rar);;ZIP File (*.zip);;7z File (*.7z)")
            self.source_dir_or_file.setText(source_dir_or_file_path[0])
 
    def start_btn_clk(self):
        self.start_btn.setEnabled(False)
        self.thread_.start()
 
    def thread_is_finished(self, text):
        if text is True:
            self.start_btn.setEnabled(True)

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class WorkThread(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
 
    def run(self):
        try:
            compress_file_type = self.parent.compress_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()
            if source_dir_or_file == '' or target_dir_or_file == '':
                self.message.emit('来源或目标文件路径为空,请检查参数设置!')
                return
            if file_catch_type == '压缩' and os.path.isfile(source_dir_or_file):
                self.message.emit('当处理类型为:压缩,来源类型应该选择文件夹,请按顺序设置参数!')
                return
            if file_catch_type == '解压缩' and os.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))
 
            if compress_file_type == 'zip格式':
                if file_catch_type == '压缩':
                    self.do_zip(source_dir_or_file, target_dir_or_file)
                else:
                    self.un_zip(source_dir_or_file, target_dir_or_file)
            elif compress_file_type == 'rar格式':
                if file_catch_type == '压缩':
                    self.message.emit('rar格式的文件压缩正在玩命开发中,请关注后续版本更新!')
                else:
                    self.un_rar(source_dir_or_file, target_dir_or_file)
            elif compress_file_type == '7z格式':
                if file_catch_type == '压缩':
                    self.do_7z(source_dir_or_file, target_dir_or_file)
                else:
                    self.un_7z(source_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)
 
    def do_zip(self, source_, target_file):
        """
        If the user selects the "压缩" option, then the user can select a directory, and the path of the directory will be
        displayed in the text box
        """
        zip_file = zip.ZipFile(target_file, 'w')
        pre_len = len(os.path.dirname(source_))
        for parent, dirnames, filenames in os.walk(source_):
            for filename in filenames:
                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()
 
    def un_zip(self, source_file, target_):
        """
        > Unzip a file to a target directory
 
        :param source_file: The file you want to unzip
        :param target_: the directory where you want to unzip the file
        """
        zip_file = zip.ZipFile(source_file)
        if os.path.isdir(target_):
            pass
        else:
            os.mkdir(target_)
        for names in zip_file.namelist():
            zip_file.extract(names, target_)
        zip_file.close()
 
    def do_7z(self, source_, target_file):
        """
        > This function takes a source file and a target file and compresses the source file into the target file using 7z
 
        :param source_: The source file or directory to be compressed
        :param target_file: The name of the file to be created
        """
        with py7zr.SevenZipFile(target_file, 'r') as file:
            file.extractall(path=source_)
 
    def un_7z(self, source_file, target_):
        """
        It takes a source directory and a target file, and creates a zip file containing the contents of the source
        directory
 
        :param source_: The path to the folder you want to zip
        :param target_file: The path to the zip file you want to create
        """
        with py7zr.SevenZipFile(source_file, 'w') as file:
            file.writeall(target_)
 
    def un_rar(self, source_file, target_):
        """
        It takes a source file and a target directory and unzips the source file into the target directory
 
        :param source_file: The path to the RAR file you want to extract
        :param target_: The directory where you want the files to be extracted to
        """
        obj_ = rar.RarFile(source_file.decode('utf-8'))
        obj_.extractall(target_.decode('utf-8'))

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

?
1
2
3
4
5
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = CompressUI()
    main.show()
    sys.exit(app.exec_())

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

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

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

原文链接:https://mp.weixin.qq.com/s/WOObNVDiM5OemLNpAIEsQw

延伸 · 阅读

精彩推荐
  • Pythonpython单向链表实例详解

    python单向链表实例详解

    这篇文章主要为大家详细介绍了python单向链表实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    python-行者11442023-02-17
  • PythonPyHacker编写URL批量采集器

    PyHacker编写URL批量采集器

    这篇文章主要为大家介绍了SpringBoot整合VUE EasyExcel实现数据导入导出,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    巡安似海6752023-01-27
  • PythonPython中判断子串存在的性能比较及分析总结

    Python中判断子串存在的性能比较及分析总结

    这篇文章主要给大家总结介绍了Python中判断子串存在的性能比较及分析的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定...

    栖迟于一丘5132021-07-18
  • PythonK近邻法(KNN)相关知识总结以及如何用python实现

    K近邻法(KNN)相关知识总结以及如何用python实现

    这篇文章主要介绍了K近邻法(KNN)相关知识总结以及如何用python实现,帮助大家更好的利用python实现机器学习,感兴趣的朋友可以了解下...

    元小疯12042021-08-30
  • Python深入理解Python3中的http.client模块

    深入理解Python3中的http.client模块

    这篇文章主要介绍了关于Python3中http.client模块的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。...

    Glumes10812020-09-27
  • PythonPython Excel处理库openpyxl详解

    Python Excel处理库openpyxl详解

    这篇文章主要介绍了Python Excel处理库openpyxl详解,需要的朋友可以参考下...

    脚本之家4952021-10-24
  • PythonPython LeNet网络详解及pytorch实现

    Python LeNet网络详解及pytorch实现

    LeNet主要用来进行手写字符的识别与分类,并在美国的银行中投入了使用。本文主要为大家详细介绍了LetNet以及通过pytorch实现LetNet,感兴趣的小伙伴可以学...

    Serins6802022-03-07
  • PythonPython爬虫爬取博客实现可视化过程解析

    Python爬虫爬取博客实现可视化过程解析

    这篇文章主要介绍了Python爬虫爬取博客实现可视化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参...

    杨万里8192020-06-29