Extract TarFile contents under archive_path/ to target_path/
(tarfileobj, target_path, archive_path)
| 212 | |
| 213 | |
| 214 | def extract_tarfile_to(tarfileobj, target_path, archive_path): |
| 215 | """Extract TarFile contents under archive_path/ to target_path/""" |
| 216 | |
| 217 | target_path = os.path.abspath(target_path) |
| 218 | |
| 219 | def get_members(): |
| 220 | for member in tarfileobj.getmembers(): |
| 221 | if archive_path: |
| 222 | norm_path = os.path.normpath(member.name) |
| 223 | if norm_path.startswith(archive_path + os.path.sep): |
| 224 | member.name = norm_path[len(archive_path)+1:] |
| 225 | else: |
| 226 | continue |
| 227 | |
| 228 | dst_path = os.path.abspath(os.path.join(target_path, member.name)) |
| 229 | if os.path.commonpath([target_path, dst_path]) != target_path: |
| 230 | # Path not under target_path, probably contains ../ |
| 231 | continue |
| 232 | |
| 233 | yield member |
| 234 | |
| 235 | tarfileobj.extractall(target_path, members=get_members()) |
| 236 | |
| 237 | |
| 238 | def make_init(dirname): |
no test coverage detected