-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathuntar.py
38 lines (31 loc) · 1.06 KB
/
untar.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
"""Unpack an uncompressed tar archive."""
import os
from utarfile import TarFile
def exists(path):
try:
_ = os.stat(path)
except:
return False
else:
return True
def untar(filename, overwrite=False, verbose=False, chunksize=4096):
with open(filename) as tar:
for info in TarFile(fileobj=tar):
if info.type == "dir":
if verbose:
print("D %s" % info.name)
name = info.name.rstrip("/")
if not exists(name):
os.mkdir(name)
elif info.type == "file":
if verbose:
print("F %s" % info.name)
if overwrite or not exists(info.name):
with open(info.name, "wb") as fp:
while True:
chunk = info.subf.read(chunksize)
if not chunk:
break
fp.write(chunk)
elif verbose:
print("? %s" % info.name)