-
Notifications
You must be signed in to change notification settings - Fork 1
/
unbox.py
53 lines (35 loc) · 1.07 KB
/
unbox.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
#!/usr/bin/python3
import os
import sys
import struct
from lib.box import get_box_entries
def unbox(fn):
with open(fn, 'rb') as f:
for name, data in get_box_entries(f):
print('Unpacking', name)
"""
initially I wrote this:
if name.startswith('/'):
name = name[1:]
I think Tiandy'd be proud of me :)
"""
while name.startswith('/'):
name = name[1:]
name = name.replace('..', '__')
fdir = os.path.dirname(name)
if fdir:
os.makedirs(fdir, exist_ok=True)
with open(name, 'wb') as wf:
wf.write(data)
if __name__ == '__main__':
if len(sys.argv) < 2:
print('python3 %s [box_file]' % sys.argv[0], file=sys.stderr)
print('python3 %s [box_file] [target_dir]' % sys.argv[0], file=sys.stderr)
exit(1)
box_file = sys.argv[1]
target_dir = sys.argv[2] if len(sys.argv) > 2 else os.path.splitext(box_file)[0]
box_file = os.path.abspath(box_file)
os.makedirs(target_dir, exist_ok=True)
os.chdir(target_dir)
print('Extracting to', target_dir)
unbox(box_file)