-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
89 lines (69 loc) · 1.53 KB
/
extract.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
#!/usr/env python
# -*- encoding: utf-8 -*-
'''
DESCRIPTION:
Extracts compressed tar.gz, tar.bz2 or zip archives.
USAGE:
python extract.py [options]
OPTIONS:
-h | --help prints this help message.
-n archive | --name=archive
specify the name of the archive to extact.
-p location | --path=location
specify the extraction location.
'''
import os
import sys
import getopt
import tarfile
import zipfile
def usage():
print(__doc__)
def unzip(zip, path):
z = zipfile.ZipFile(zip)
for name in z.namelist():
(dirname, filename) = os.path.split(name)
if filename == '': # directory
dir = os.path.join(path, dirname)
if not os.path.exists(dir):
os.mkdir(dir)
else: # file
fname = os.path.join(path, name)
print(fname)
fd = open(fname, 'wb')
fd.write(z.read(name))
fd.close()
z.close()
def untar(tar, path):
if name.endswith('.gz'):
compression = 'gz'
else:
compression = 'bz2'
t = tarfile.open(name, 'r:%s' % compression)
for member in t.getmembers():
print(member.name)
t.extract(member, path=path)
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'hn:p:', ['help', 'name=', 'path='])
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(2)
name = None
path = '.'
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-n', '--name'):
name = a
elif o in ('-p', '--path'):
path = a
if not name:
usage()
sys.exit(2)
if name.endswith('.zip'):
unzip(name, path)
else:
untar(name, path)