一文详解如何用GPU来运行Python代码/基于Python自制一个文件解压缩小工具 | 您所在的位置:网站首页 › 用7zip如何解压文件 › 一文详解如何用GPU来运行Python代码/基于Python自制一个文件解压缩小工具 |
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')) |
CopyRight 2018-2019 实验室设备网 版权所有 |