-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzipFile.py
185 lines (171 loc) · 6.15 KB
/
unzipFile.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
import zipfile, glob, os, sys, getopt
password = [""]
link = ""
folderName = "Extracted"
def setPassword(arrayOfPassword):
global password
index = 1
for pwd in arrayOfPassword:
password.insert(index, pwd)
index = index + 1
def setLink(link_):
global link
link = link_
def setFolderName(folder_Name):
global folderName
folderName = folder_Name
def getPassword():
return password
def getFolderName():
return folderName
def getLink():
return link
def extract(fileName):
print(fileName)
os.chdir(link)
estratto = False
if(zipfile.is_zipfile(fileName)):
zf = zipfile.ZipFile(fileName)
if(checkIfZipIsProtected(fileName)):
if(len(password) > 1):
if(not checkIfFileIsAlreadyExtracted(fileName)):
for i in range(1, len(password)):
estratto = False
try:
zf.extractall("../" + folderName, pwd=password[i])
estratto = True
break
except:
if(not estratto and i == (len(password) -1 )):
print("\nNon e' stata fornita la password per " + fileName + "\n")
else:
pass
if(not estratto):
print("\nNon sono riuscto ad estrarre " + fileName + "\n")
else:
print("\n" + fileName + " estratto con successo" + "\n")
else:
print("\n" + fileName + " gia presente. Non sara estratto" + "\n")
else:
print("\nSono presenti dei .zip protetti. Inserire la password." + "\n")
sys.exit()
else:
if(not checkIfFileIsAlreadyExtracted(fileName)):
print("\nProvo a decomprimere " + fileName + "\n")
try:
zf.extractall("../" + folderName, pwd=password[0])
print("\n" + fileName + " estratto con successo" + "\n")
except:
print("Non sono riuscto ad estrarre " + fileName)
else:
print("\n" + fileName + " gia presente. Non sara estratto" + "\n")
else:
pass
os.chdir("..")
return estratto
def extractAll(pathToFolder):
os.chdir(pathToFolder)
estratto = False
for zip in glob.glob('*.zip'):
if(zipfile.is_zipfile(zip)):
zf = zipfile.ZipFile(zip)
if(checkIfZipIsProtected(zip)):
if(len(password) > 1):
if(not checkIfFileIsAlreadyExtracted(zip)):
print("\n" + "Provo a decomprimere " + zip)
for i in range(1, len(password)):
estratto = False
try:
zf.extractall("../" + folderName, pwd=password[i])
estratto = True
break
except:
if(not estratto and i == (len(password) -1 )):
print("\n" + "Non e' stata fornita la password per " + zip + "\n")
else:
pass
if(not estratto):
print("\n" + "Non sono riuscto ad estrarre " + zip + "\n")
else:
print("\n" + zip + " estratto con successo" + "\n")
else:
print("\n" + zip + " gia presente. Non sara estratto" + "\n")
else:
print("\n" + "Sono presenti dei .zip protetti. Inserire la password." + "\n")
sys.exit()
else:
if(not checkIfFileIsAlreadyExtracted(zip)):
print("\n" + "Provo a decomprimere " + zip + "\n")
try:
zf.extractall("../" + folderName, pwd=password[0])
print("\n" + zip + " estratto con successo" + "\n")
except:
print("\n" + "Non sono riuscto ad estrarre " + zip)
else:
print("\n" + zip + " gia presente. Non sara estratto" + "\n")
else:
pass
os.chdir("..")
return estratto
def checkIfZipIsProtected(zip):
zf = zipfile.ZipFile(zip)
for zip_info in zf.infolist():
isEncrypted = zip_info.flag_bits & 0x1
if(isEncrypted):
return True
else:
return False
def checkIfFileIsAlreadyExtracted(zip):
previousPath = os.getcwd()
zf = zipfile.ZipFile(zip)
os.chdir("..")
os.chdir(os.getcwd() + "/" + folderName + "/")
trovato = False
for zipFile in zf.namelist():
trovato = False
for file in glob.glob('*'):
if(zipFile == file):
trovato = True
break
else:
pass
if(not trovato):
break
else:
pass
os.chdir(previousPath)
return trovato
def createFolder(folderName):
os.chdir(os.getcwd())
if not os.path.exists(folderName):
os.makedirs(folderName)
def main(argv):
global password
global link
global folderName
pwdInserted = ""
if(len(argv) == 1 and argv[0] == "--help"):
helpFile = open("helpUF.txt")
for line in helpFile:
print (line)
sys.exit(2)
try:
opts, argv = getopt.getopt(argv, "", ("path=", "password=", "folder="))
except getopt.GetoptError as err:
print(str(err))
sys.exit(2)
for opt, arg in opts:
if(opt == "--path"):
link = arg
elif(opt == "--password"):
pwdInserted = arg.split(",")
elif(opt == "--folder"):
folderName = arg
index = 1
for i in range(0, len(pwdInserted)):
password.insert(index, pwdInserted[i])
index = index + 1
createFolder(folderName)
extractAll(link)
if __name__ == '__main__':
main(sys.argv[1:])