An error occurred while loading the file. Please try again.
-
SPeillet authorede364db2f
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
import getopt
import os
import sys
import glob
import zipfile
def unzip(argv):
try:
opts, args = getopt.getopt(argv, 'ro:')
except getopt.GetoptError as err:
print(str(err))
od = args[-1]
removeZip = False
for opt, val in opts:
if opt == '-o':
od = val
elif opt == '-r':
removeZip = True
#Unzip
zipFiles = glob.glob(args[0]+os.sep+'*.zip')
for zf in zipFiles:
zfile = zipfile.ZipFile(zf, 'r')
for i in zfile.namelist():
if os.path.isdir(i) or i[-1] == '/':
try :
os.makedirs(od+os.sep+i)
except:
pass
else :
try: os.makedirs(od + os.sep + os.path.dirname(i))
except: pass
file = zfile.read(i)
fp = open(od+os.sep+i, "w+b")
fp.write(file)
fp.close()
zfile.close()
if removeZip:
os.remove(zf)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit(
'Usage: python batchUnzip.py [-o <output folder - def. zip folder>] [-r] <folder containing zip files>\n'
'Searches for all available zip file and unzip them all.')
else:
zipFiles = glob.glob(sys.argv[-1] + '/*.zip')
if len(zipFiles) > 0:
unzip(sys.argv[1:])
else:
sys.exit(
'No zip file found in the given folder\n'
'Usage: python batchUnzip.py [-o <output folder - def. zip folder>] [-r] <folder containing zip files>\n')