Changed the code (you must close the ZipFile before deleting it + add the extraction of internal zip files):
import os import shutil import tempfile from zipfile import ZipFile def unzip_recursively(parent_archive): parent_archive = ZipFile(parent_archive) result = {} tmpdir = tempfile.mkdtemp() try: parent_archive.extractall(path=tmpdir) namelist=parent_archive.namelist() for name in namelist[1:]: innerzippath = os.path.join(tmpdir, name) inner_zip = ZipFile(innerzippath) inner_extract_path = innerzippath+'.content' if not os.path.exists(inner_extract_path): os.makedirs(inner_extract_path) inner_zip.extractall(path=inner_extract_path) for inner_file_name in inner_zip.namelist(): result[inner_file_name] = open(os.path.join(inner_extract_path, inner_file_name)).read() inner_zip.close() finally: shutil.rmtree(tmpdir) return result if __name__ == '__main__': print unzip_recursively('file.zip')
source share