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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
import zipfile
import os
import tkinter import tkinter.filedialog import tkinter.messagebox import tkinter.simpledialog
class Zip_file: file_list = []
def __init__(self): self.logic()
def logic(self): root = tkinter.Tk() root.minsize(300, 300) btn_add = tkinter.Button(root, text='添加文件到列表', command=self.add_file) btn_add.grid(row=1, column=0) btn_ys = tkinter.Button(root, text='开始压缩', command=self.press_file) btn_ys.grid(row=1, column=1) btn_jy = tkinter.Button(root, text='开始解压', command=self.depress_file) btn_jy.grid(row=1, column=2) btn_jy = tkinter.Button(root, text='清空当前文件列表', command=self.clear_file) btn_jy.grid(row=3, column=0, columnspan = 3)
self.val = tkinter.StringVar() self.val.set(' 暂无文件信息 ') label1 = tkinter.Label(root, textvariable=self.val, bg='pink') label1.grid(row=2, column=0, columnspan=3)
self.val2 = tkinter.StringVar() self.val2.set(' 压缩或解压前请先添加文件到列表 ') label2 = tkinter.Label(root, textvariable=self.val2, bg='yellow') label2.grid(row=0, column=0, columnspan = 3)
root.mainloop()
def clear_file(self): self.file_list.clear() self.val.set('暂无文件信息!')
def add_file(self): print('添加文件') file_name_temp = list(tkinter.filedialog.askopenfilenames(title='请选择要压缩的文件')) if file_name_temp: pass else: return self.file_list += file_name_temp file_set = set(self.file_list) file_set_list = list(file_set) tkinter.messagebox.showinfo('提示', '文件添加成功,可在列表中查看') for i in file_set: print(i)
self.val.set('\n'.join(file_set_list)) pass
def press_file(self): print('压缩文件')
self.des_path = tkinter.filedialog.askdirectory(title='请选择压缩包保存的路径') if self.des_path: pass else: return print('压缩的路径是:') print(self.des_path)
file_name = tkinter.simpledialog.askstring(\ title='获取信息', prompt='给你的新压缩包起个名字吧', initialvalue='demo') file_name = file_name + '.zip' print(file_name)
if os.path.exists(self.des_path+'/'+file_name):
result = tkinter.messagebox.askquestion(title='系统提醒', message='文件已存在,是否替换原文件') print(result) if result == 'yes': print('用户选择的是') pass else: tkinter.messagebox.showwarning(title='警告', message='请重新选择目录') self.press_file() else: pass
zp = zipfile.ZipFile(self.des_path+'/'+file_name, 'w',zipfile.ZIP_DEFLATED) for i in set(self.file_list): zp.write(i,self.get_file_name(i)[1]) else: tkinter.messagebox.showinfo(\ '提示', '压缩文件%s保存成功' % self.des_path+'/'+file_name) zp.close() self.clear_file()
pass
def depress_file(self): print('解压文件') src_path_list = set(self.file_list)
des_path = tkinter.filedialog.askdirectory(title='请选择解压后文件保存的路径')
if des_path: pass else: return
for src_path in src_path_list: zp = zipfile.ZipFile(src_path,'r') for file in zp.namelist(): print(file) zp.extract(file,des_path) else: tkinter.messagebox.showinfo('提示', '解压缩文件%s成功' % src_path) zp.close() self.clear_file() pass
def get_file_name(self,path): import os new_name = os.path.split(path) return new_name
z = Zip_file()
|