I am writing a program in python and using tarfile
to extract tarfiles. Some of these tarfiles contain folders which start with a /
or (Alternatively for windows \
) which cause problems (files are extracted to wrong place). How can I get around this issue and make sure that the extraction ends up in correct place ?
The docs for tarfile
explicitly warn about such a scenario. Instead you need to iterate over the content of the tar file and extract
each file individually:
import os
import tarfile
extract_to = "."
tfile = tarfile.open('so.tar')
members = tfile.getmembers()
for m in members:
if m.name[0] == os.sep:
m.name = m.name[1:]
tfile.extract(m, path=extract_to)