当前位置:   article > 正文

python压缩与解压zip和7z格式_python解压7z文件

python解压7z文件
  1. import os
  2. import tkinter
  3. import tkinter.filedialog
  4. import tkinter.messagebox # 弹出消息框
  5. import tkinter.simpledialog
  6. import tkinter.simpledialog
  7. import zipfile
  8. from tkinter import Label, Button,messagebox
  9. import pyautogui
  10. from py7zr import py7zr
  11. from pymsgbox import confirm
  12. class ys():
  13. # path:要压缩的文件的
  14. def z7z_wjj(self, path):
  15. try:
  16. z7z_name = path + '.7z' # 获取绝对路径然后给压缩文件加上.7z结尾
  17. sf_jm = messagebox.askyesno('加密', '是否加密') # 选择是否加密
  18. if sf_jm == True:
  19. mm = tkinter.simpledialog.askstring(title='加密', prompt='请设置密码:') # 获取密码
  20. # mm = pyautogui.prompt(text='设置密码', title='加密压缩')
  21. z = py7zr.SevenZipFile(z7z_name, 'w', password=mm.encode('gbk').decode('gbk')) # 设置密码
  22. for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
  23. fpath = dirpath.replace(path, '') # 获取目录路径
  24. fpath = fpath and fpath + os.sep or ''
  25. for filename in filenames: # 迭代文件名
  26. z.write(os.path.join(dirpath, filename),fpath + filename) # 写入
  27. z.close() # 关闭资源,以免占用内存
  28. return True
  29. elif sf_jm == False:
  30. z = py7zr.SevenZipFile(z7z_name, 'w')
  31. for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
  32. fpath = dirpath.replace(path, '') # 获取目录路径
  33. fpath = fpath and fpath + os.sep or ''
  34. for filename in filenames: # 迭代文件名
  35. z.write(os.path.join(dirpath, filename), fpath + filename) # 写入
  36. z.close() # 关闭资源,以免占用内存
  37. return True
  38. except:
  39. return False
  40. def zip_wjj(self, path):
  41. try:
  42. zip_name = path + '.zip' # 获取绝对路径然后给压缩文件加上.zip结尾
  43. z = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) # 写入.zip
  44. for dirpath, dirnames, filenames in os.walk(path): # 使用os遍历目录路径,目录名,文件名
  45. fpath = dirpath.replace(path, '') # 获取目录路径
  46. fpath = fpath and fpath + os.sep or ''
  47. for filename in filenames: # 迭代文件名
  48. z.write(os.path.join(dirpath, filename), fpath + filename) # 写入
  49. z.close() # 关闭资源,以免占用内存
  50. return True
  51. except:
  52. return False
  53. def ys_wjj(self, path):
  54. xz = pyautogui.confirm(text='请选择压缩格式', buttons=['zip', '7z']) # 选择压缩格式
  55. if xz == 'zip':
  56. fhz = self.zip_wjj(path) # fhz:返回值
  57. if fhz:
  58. tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
  59. elif fhz == False:
  60. tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
  61. else:
  62. fhz = self.z7z_wjj(path)
  63. if fhz:
  64. tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
  65. elif fhz == False:
  66. tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
  67. def _zip_wj_(self, wj_path): # zip压缩
  68. try:
  69. for path in wj_path: # 遍历路径
  70. zip_name = path[:str(path).rfind('.')] + '.zip'
  71. zip_lj = path[str(path).rfind('/')+1:] #获取文件名
  72. z = zipfile.ZipFile(zip_name, 'w')
  73. z.write(path, zip_lj)
  74. z.close()
  75. return True
  76. except:
  77. return False
  78. def _7z_wj(self, wj_path):#7z压缩
  79. jm = messagebox.askyesno('加密', '是否加密') # 选择框
  80. if jm:
  81. mm = tkinter.simpledialog.askstring(title='加密', prompt='请设置密码:') # 获取密码
  82. try:
  83. for path in wj_path: # 遍历路径
  84. path1 = path[:str(path).find('.')] + '.7z'
  85. file_name = path[str(path).rfind('/')+1:]
  86. try:
  87. z = py7zr.SevenZipFile(path1, 'w', password=mm.encode('gbk').decode('gbk')) # 设置密码
  88. z.writeall(path,file_name)
  89. z.close()
  90. except:
  91. return False
  92. return True
  93. except:
  94. return False
  95. elif not jm:
  96. try:
  97. for path in wj_path:
  98. path1 = path[:str(path).find('.')] + '.7z'
  99. file_name = path[str(path).rfind('/')+1:]
  100. try:
  101. z = py7zr.SevenZipFile(path1, 'w')
  102. z.writeall(path,file_name)
  103. z.close()
  104. except:
  105. return False
  106. return True
  107. except:
  108. return False
  109. def ys_wj(self, path): # 压缩文件
  110. xz = confirm(text='请选择压缩格式', buttons=['zip', '7z']) # 选择框
  111. if xz == 'zip': # 选择zip 调用_zip_wj_()
  112. if self._zip_wj_(path):
  113. tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
  114. else:
  115. tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
  116. elif xz == '7z': # 选择7z 调用_7z_wj_()
  117. if self._7z_wj(path):
  118. tkinter.messagebox.askokcancel(message="压缩成功") # 提示压缩成功
  119. else:
  120. tkinter.messagebox.askokcancel(message="压缩失败") # 提示压缩失败
  121. class jy():
  122. def un_7z(self, file): # 解压7z文件
  123. z = py7zr.SevenZipFile(file, 'r') # 打开7z文件
  124. if len(z.getnames()) > 1: # 判断压缩文件压缩的是不是文件夹
  125. fime_name = file[:file.rfind('.')] # 是文件夹则解压成文件夹
  126. else:
  127. fime_name = file[:file.rfind('/')] # 是文件则解压成文件
  128. if z.needs_password(): # 判断7z文件是否需要密码
  129. z.close() # 关闭
  130. while True:
  131. mima = pyautogui.password(text='请输入密码', title='解压', default='', mask='*')
  132. try:
  133. z = py7zr.SevenZipFile(file, 'r', password=mima.encode('gbk').decode('gbk')) # 密码
  134. z.extractall(fime_name)
  135. z.close()
  136. return True
  137. except:
  138. xz = pyautogui.confirm(text='密码错误', buttons=['重试', '取消'])
  139. if xz != '重试':
  140. return False
  141. else:
  142. try:
  143. z.extractall(fime_name)
  144. z.close()
  145. return True
  146. except:
  147. return False
  148. def un_zip(self, zip):
  149. try:
  150. z = zipfile.ZipFile(zip, 'r') # 打开zip文件
  151. if len(z.namelist()) > 1: # 判断压缩文件压缩的是不是文件夹
  152. zip_file = zip[:zip.rfind('.')] # 是文件夹则解压成文件夹
  153. else:
  154. zip_file = zip[:zip.rfind('/')] # 是文件则解压成文件
  155. z.extractall(zip_file) # 解压
  156. z.close() # 关闭
  157. return True
  158. except:
  159. return False
  160. def jy(self, path):
  161. for file in path: # 遍历文件路径
  162. if zipfile.is_zipfile(file) and file[str(file).rfind('.') + 1:] == 'zip': # 判断是不是zip文件
  163. if self.un_zip(file):
  164. tkinter.messagebox.askokcancel(message="解压成功") # 提示解压成功
  165. else:
  166. tkinter.messagebox.askokcancel(message="解压失败") # 提示解压失败
  167. elif py7zr.is_7zfile(file) and file[str(file).rfind('.') + 1:] == '7z': # 判断是不是7z文件
  168. if self.un_7z(file):
  169. tkinter.messagebox.askokcancel(message="解压成功") # 提示解压成功
  170. else:
  171. tkinter.messagebox.askokcancel(message="解压失败") # 提示解压失败
  172. else:
  173. tkinter.messagebox.askokcancel(message="暂时不能解压该类型文件") # 提示框
  174. def main():
  175. root = tkinter.Tk() # 主窗口
  176. def ys_wjj():
  177. wjj = tkinter.filedialog.askdirectory() # 文件夹选择框
  178. if wjj is None:
  179. return # 如果没有选择结束
  180. elif wjj:
  181. ys().ys_wjj(wjj) # 压缩选中的文件夹
  182. def ys_wj():
  183. wj = tkinter.filedialog.askopenfilenames() # 多文件选择框
  184. if wj is None:
  185. return # 如果没有选择结束
  186. elif wj:
  187. ys().ys_wj(wj) # 压缩选中的文件
  188. def jy_wj():
  189. xz = tkinter.filedialog.askopenfilenames() # 多文件选择框
  190. if xz is None:
  191. return # 如果没有选择结束
  192. elif xz:
  193. jy().jy(xz) # 选中的文件如果是zip文件则进行解压
  194. # label标签 grid括号中的row代表label是放在第几行,column是放在第几列
  195. # Button操作按钮
  196. Label(root, text="压缩文件夹:").grid(row=0, column=0)
  197. Button(root, text="压缩文件夹", command=ys_wjj).grid(row=0, column=1)
  198. Label(root, text="压缩文件:").grid(row=1, column=0)
  199. Button(root, text="压缩文件", command=ys_wj).grid(row=1, column=1)
  200. Label(root, text="解压文件:").grid(row=2, column=0)
  201. Button(root, text="解压文件", command=jy_wj).grid(row=2, column=1)
  202. Label(root, text="退出系统:").grid(row=3, column=0)
  203. Button(root, text="退出系统", command=root.quit).grid(row=3, column=1)
  204. root.mainloop() # 界面进入消息循环
  205. if __name__ == '__main__':
  206. main()

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号