赞
踩
- import os
- import tkinter
- import tkinter.filedialog
- import tkinter.messagebox # 弹出消息框
- import tkinter.simpledialog
- import tkinter.simpledialog
- import zipfile
- from tkinter import Label, Button,messagebox
- import pyautogui
- from py7zr import py7zr
- from pymsgbox import confirm
- class ys():
- # path:要压缩的文件的
- def z7z_wjj(self, path):
- try:
- z7z_name = path + '.7z' # 获取绝对路径然后给压缩文件加上.7z结尾
- sf_jm = messagebox.askyesno('加密', '是否加密') # 选择是否加密
- if sf_jm == True:
- mm = tkinter.simpledialog.askstring(title='加密', prompt='请设置密码:') # 获取密码
- # mm = pyautogui.prompt(text='设置密码', title='加密压缩')
- z = py7zr.SevenZipFile(z7z_name, 'w', password=mm.encode('gbk').decode('gbk')) # 设置密码
- for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
- fpath = dirpath.replace(path, '') # 获取目录路径
- fpath = fpath and fpath + os.sep or ''
- for filename in filenames: # 迭代文件名
- z.write(os.path.join(dirpath, filename),fpath + filename) # 写入
- z.close() # 关闭资源,以免占用内存
- return True
- elif sf_jm == False:
- z = py7zr.SevenZipFile(z7z_name, 'w')
- for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
- fpath = dirpath.replace(path, '') # 获取目录路径
- fpath = fpath and fpath + os.sep or ''
- for filename in filenames: # 迭代文件名
- z.write(os.path.join(dirpath, filename), fpath + filename) # 写入
- z.close() # 关闭资源,以免占用内存
- return True
- except:
- return False
-
- def zip_wjj(self, path):
- try:
- zip_name = path + '.zip' # 获取绝对路径然后给压缩文件加上.zip结尾
- z = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) # 写入.zip
- for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
- fpath = dirpath.replace(path, '') # 获取目录路径
- fpath = fpath and fpath + os.sep or ''
- for filename in filenames: # 迭代文件名
- z.write(os.path.join(dirpath, filename), fpath + filename) # 写入
- z.close() # 关闭资源,以免占用内存
- return True
- except:
- return False
-
- def ys_wjj(self, path):
- xz = pyautogui.confirm(text='请选择压缩格式', buttons=['zip', '7z']) # 选择压缩格式
- if xz == 'zip':
- fhz = self.zip_wjj(path) # fhz:返回值
- if fhz:
- tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
- elif fhz == False:
- tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
- else:
- fhz = self.z7z_wjj(path)
- if fhz:
- tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
- elif fhz == False:
- tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
-
- def _zip_wj_(self, wj_path): # zip压缩
- try:
- for path in wj_path: # 遍历路径
- zip_name = path[:str(path).rfind('.')] + '.zip'
- zip_lj = path[str(path).rfind('/')+1:] #获取文件名
- z = zipfile.ZipFile(zip_name, 'w')
- z.write(path, zip_lj)
- z.close()
- return True
- except:
- return False
-
- def _7z_wj(self, wj_path):#7z压缩
- jm = messagebox.askyesno('加密', '是否加密') # 选择框
- if jm:
- mm = tkinter.simpledialog.askstring(title='加密', prompt='请设置密码:') # 获取密码
- try:
- for path in wj_path: # 遍历路径
- path1 = path[:str(path).find('.')] + '.7z'
- file_name = path[str(path).rfind('/')+1:]
- try:
- z = py7zr.SevenZipFile(path1, 'w', password=mm.encode('gbk').decode('gbk')) # 设置密码
- z.writeall(path,file_name)
- z.close()
- except:
- return False
- return True
- except:
- return False
- elif not jm:
- try:
- for path in wj_path:
- path1 = path[:str(path).find('.')] + '.7z'
- file_name = path[str(path).rfind('/')+1:]
- try:
- z = py7zr.SevenZipFile(path1, 'w')
- z.writeall(path,file_name)
- z.close()
- except:
- return False
- return True
- except:
- return False
-
- def ys_wj(self, path): # 压缩文件
- xz = confirm(text='请选择压缩格式', buttons=['zip', '7z']) # 选择框
- if xz == 'zip': # 选择zip 调用_zip_wj_()
- if self._zip_wj_(path):
- tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
- else:
- tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
- elif xz == '7z': # 选择7z 调用_7z_wj_()
- if self._7z_wj(path):
- tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
- else:
- tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
-
-
- class jy():
- def un_7z(self, file): # 解压7z文件
- z = py7zr.SevenZipFile(file, 'r') # 打开7z文件
- if len(z.getnames()) > 1: # 判断压缩文件压缩的是不是文件夹
- fime_name = file[:file.rfind('.')] # 是文件夹则解压成文件夹
- else:
- fime_name = file[:file.rfind('/')] # 是文件则解压成文件
- if z.needs_password(): # 判断7z文件是否需要密码
- z.close() # 关闭
- while True:
- mima = pyautogui.password(text='请输入密码', title='解压', default='', mask='*')
- try:
- z = py7zr.SevenZipFile(file, 'r', password=mima.encode('gbk').decode('gbk')) # 密码
- z.extractall(fime_name)
- z.close()
- return True
- except:
- xz = pyautogui.confirm(text='密码错误', buttons=['重试', '取消'])
- if xz != '重试':
- return False
- else:
- try:
- z.extractall(fime_name)
- z.close()
- return True
- except:
- return False
-
- def un_zip(self, zip):
- try:
- z = zipfile.ZipFile(zip, 'r') # 打开zip文件
- if len(z.namelist()) > 1: # 判断压缩文件压缩的是不是文件夹
- zip_file = zip[:zip.rfind('.')] # 是文件夹则解压成文件夹
- else:
- zip_file = zip[:zip.rfind('/')] # 是文件则解压成文件
- z.extractall(zip_file) # 解压
- z.close() # 关闭
- return True
- except:
- return False
-
- def jy(self, path):
- for file in path: # 遍历文件路径
- if zipfile.is_zipfile(file) and file[str(file).rfind('.') + 1:] == 'zip': # 判断是不是zip文件
- if self.un_zip(file):
- tkinter.messagebox.askokcancel(message="解压成功") # 提示解压成功
- else:
- tkinter.messagebox.askokcancel(message="解压失败") # 提示解压失败
- elif py7zr.is_7zfile(file) and file[str(file).rfind('.') + 1:] == '7z': # 判断是不是7z文件
- if self.un_7z(file):
- tkinter.messagebox.askokcancel(message="解压成功") # 提示解压成功
- else:
- tkinter.messagebox.askokcancel(message="解压失败") # 提示解压失败
- else:
- tkinter.messagebox.askokcancel(message="暂时不能解压该类型文件") # 提示框
-
-
- def main():
- root = tkinter.Tk() # 主窗口
- def ys_wjj():
- wjj = tkinter.filedialog.askdirectory() # 文件夹选择框
- if wjj is None:
- return # 如果没有选择结束
- elif wjj:
- ys().ys_wjj(wjj) # 压缩选中的文件夹
-
- def ys_wj():
- wj = tkinter.filedialog.askopenfilenames() # 多文件选择框
- if wj is None:
- return # 如果没有选择结束
- elif wj:
- ys().ys_wj(wj) # 压缩选中的文件
-
- def jy_wj():
- xz = tkinter.filedialog.askopenfilenames() # 多文件选择框
- if xz is None:
- return # 如果没有选择结束
- elif xz:
- jy().jy(xz) # 选中的文件如果是zip文件则进行解压
- # label标签 grid括号中的row代表label是放在第几行,column是放在第几列
- # Button操作按钮
- Label(root, text="压缩文件夹:").grid(row=0, column=0)
- Button(root, text="压缩文件夹", command=ys_wjj).grid(row=0, column=1)
- Label(root, text="压缩文件:").grid(row=1, column=0)
- Button(root, text="压缩文件", command=ys_wj).grid(row=1, column=1)
- Label(root, text="解压文件:").grid(row=2, column=0)
- Button(root, text="解压文件", command=jy_wj).grid(row=2, column=1)
- Label(root, text="退出系统:").grid(row=3, column=0)
- Button(root, text="退出系统", command=root.quit).grid(row=3, column=1)
- root.mainloop() # 界面进入消息循环
-
-
- if __name__ == '__main__':
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。