Python script for flattening extracted tree
The script written bellow extracts zip file and moves files contained within topmost directory out of it to the current working directory. This quick script is tailored to suit this particular question where there is one single topmost directory that contains all the files, although with a few edits can be made suitable for more general cases.
#!/usr/bin/env python3
import sys
import os
from zipfile import PyZipFile
for zip_file in sys.argv[1:]:
pzf = PyZipFile(zip_file)
namelist=pzf.namelist()
top_dir = namelist[0]
pzf.extractall(members=namelist[1:])
for item in namelist[1:]:
rename_args = [item,os.path.basename(item)]
print(rename_args)
os.rename(*rename_args)
os.rmdir(top_dir)
Test run
Here's an example of how the script is supposed to work. Everything extracted to current working directory, but source file can be in differet directory altogether. The test is performed on the zip archive of my personal github repository.
$ ls
flatten_zip.py* master.zip
$ ./flatten_zip.py master.zip
['utc-time-indicator-master/.gitignore', '.gitignore']
['utc-time-indicator-master/LICENSE', 'LICENSE']
['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator']
['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png']
$ ls
flatten_zip.py* LICENSE master.zip utc_indicator.png utc-time-indicator
Test with source file being in different location
$ mkdir test_unzip
$ cd test_unzip
$ ../flatten_zip.py ../master.zip
['utc-time-indicator-master/.gitignore', '.gitignore']
['utc-time-indicator-master/LICENSE', 'LICENSE']
['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator']
['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png']
$ ls
LICENSE utc_indicator.png utc-time-indicator
unzip archive.zip && mv Release/* .
– George Udosen Mar 12 '17 at 0:48Release
folder – jsta Jun 8 '17 at 17:47