-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zstd DC.py
201 lines (165 loc) · 6.91 KB
/
Zstd DC.py
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
import zstd
import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd
from collections import deque
class Window:
def __init__(self, master):
self.master = master
self.master.option_add("*Font", "Verdana 12")
self.Main = Frame(self.master)
self.stack = deque(maxlen = 10)
self.stackcursor = 0
self.L1 = Label(self.Main, text = "Zstd DC - Zstandard (De-)Compresser v1.0")
self.L1.pack(padx = 5, pady = 0)
self.menu = Menu(self.Main)
filemenu = Menu(self.menu, tearoff=0)
filemenu.add_command(label="Compress", command = self.Compress, font = ("Verdana", 10))
filemenu.add_command(label="Decompress", command = self.Decompress, font = ("Verdana", 10))
filemenu.add_command(label="Mass compress", command = self.Mcompress, font = ("Verdana", 10))
filemenu.add_command(label="Mass decompress", command = self.Mdecompress, font = ("Verdana", 10))
filemenu.add_command(label="Exit", command = root.destroy, font = ("Verdana", 10))
self.menu.add_cascade(label="File",
menu=filemenu)
self.menu.add_command(label = "About", command = self.About)
self.master.config(menu = self.menu)
self.B1 = Button(self.Main, text = "Decompress", width = 12, command = self.Decompress)
self.B1.pack(padx = 5, pady = 5, side = LEFT)
self.B2 = Button(self.Main, text = "Compress", width = 12, command = self.Compress)
self.B2.pack(padx = 5, pady = 5, side = RIGHT)
self.B3 = Button(self.Main, text = "Mass decompress", width = 15, command = self.Mdecompress)
self.B3.pack(padx = 5, pady = 5, side = LEFT)
self.B4 = Button(self.Main, text = "Mass compress", width = 15, command = self.Mcompress)
self.B4.pack(padx = 5, pady = 5, side = RIGHT)
self.L2 = Label(self.Main, text="""Compression level (1-9)""")
self.L3 = Label(self.Main, text="""Extension (mass only)""")
self.T1 = Text(root, height=1, width = 22)
self.T2 = Text(root, height=1, width = 22)
self.Main.pack(padx = 15, pady = 15)
self.T1.pack(padx=5, pady=5)
self.T1.insert(tk.END, "Compression level (1-22)")
self.T1.bind("<Button-1>", lambda args: self.T1.delete('1.0', END))
self.T2.pack(padx=5, pady=5)
self.T2.insert(tk.END, "Extension (mass only)")
self.T2.bind("<Button-1>", lambda args: self.T2.delete('1.0', END))
def Compress(self):
try:
levelc=int(self.T1.get(1.0, "end-1c"))
except:
levelc=3
if levelc <= -100:
levelc = -100
if levelc > 22:
levelc = 22
ftypes = [('All files', '*')]
filename = fd.askopenfilename(filetypes=ftypes)
if filename != "" and filename is not None:
with open(filename, 'rb') as myfile:
f = myfile.read()
ftypes = [('All files', '*')]
newfilename = fd.asksaveasfilename(filetypes=ftypes, initialfile=filename)
z = open(newfilename, "ab")
z.write(zstd.compress(f,levelc))
tk.messagebox.showinfo(title='Success!', message='File compressed successfully!')
myfile.close()
def Decompress(self):
ftypes = [('All files', '*')]
filename = fd.askopenfilename(filetypes=ftypes)
if filename != "" and filename is not None:
with open(filename, 'rb') as myfile:
f = myfile.read()
ftypes = [('All files', '*')]
newfilename = fd.asksaveasfilename(filetypes=ftypes, initialfile=filename)
z = open(newfilename, "ab")
z.write(zstd.decompress(f))
tk.messagebox.showinfo(title='Success!', message='File decompressed successfully!')
myfile.close()
def Mdecompress(self):
endel = None
try:
endi=self.T2.get(1.0, "end-1c")
except:
endi=".txt"
if endi == "" or endi is None:
endi=".txt"
if endi.find(".")== -1 and not endi.startswith("-"):
endi=".txt"
if endi.startswith("-"):
endel = True
endi = endi.replace("-", "")
ftypes = [('All files', '*')]
filename = fd.askopenfilenames(filetypes=ftypes)
if filename != "" and filename is not None:
fnc = len(filename)
while fnc != 0:
fnz = filename[fnc-1].split(',')
fnz2 = fnz[0]
with open(fnz2, 'rb') as myfile:
f = myfile.read()
if endel is None or endel is not True:
newfilename = fnz2+endi
else:
newfilename = fnz2.replace(endi, "")
z = open(newfilename, "ab")
z.write(zstd.decompress(f))
fnc = fnc-1
myfile.close()
tk.messagebox.showinfo(title='Success!', message='Files decompressed successfully!')
else:
return
def Mcompress(self):
endel = None
try:
endi=self.T2.get(1.0, "end-1c")
except:
endi=".zst"
if endi == "" or endi is None:
endi=".zst"
if endi.find(".")== -1 and not endi.startswith("-"):
endi=".zst"
if endi.startswith("-"):
endel = True
endi = endi.replace("-", "")
try:
levelc=int(self.T1.get(1.0, "end-1c"))
except:
levelc=3
if levelc <= -100:
levelc = -100
if levelc > 22:
levelc = 22
ftypes = [('All files', '*')]
filename = fd.askopenfilenames(filetypes=ftypes)
if filename != "" and filename is not None:
fnc = len(filename)
while fnc != 0:
fnz = filename[fnc-1].split(',')
fnz2 = fnz[0]
with open(fnz2, 'rb') as myfile:
f = myfile.read()
if endel is None or endel is not True:
newfilename = fnz2+endi
else:
newfilename = fnz2.replace(endi, "")
z = open(newfilename, "ab")
z.write(zstd.compress(f,levelc))
fnc = fnc-1
myfile.close()
tk.messagebox.showinfo(title='Success!', message='Files decompressed successfully!')
else:
return
def About(self):
tk.messagebox.showinfo(title='About', message='''This is a program to compress and decompress files, using Zstandard.
zstd was written by Yann Collet at Facebook.
Program author: dzhemvrot
Program version: 1.0
Program restributed using GPL-3.0 license''')
root = Tk()
root.resizable(False, False)
window = Window(root)
try:
root.iconbitmap('icon.ico')
except:
pass
root.title(u'Zstd DC')
root.mainloop()