Unpack archives recursively, store (file name, extracted contents) in a dictionary

Could you help me write a return function:

dict("file1.txt": list(<contents of file1>), "file2.txt": list(<contents of file2>), "file3.txt": list(<contents of file3>), "file4.txt": list(<contents of file4>)) 

At the entrance:

  file.zip: outer\ outer\inner1.zip: file1.txt file2.txt outer\inner2.zip: file3.txt file4.txt 

My attempts (with exceptions below):

+4
source share
2 answers

Finally it worked ... with a little help: Extracting a zip file into memory? ;

 from zipfile import ZipFile, is_zipfile def extract_zip(input_zip): input_zip=ZipFile(input_zip) return {name: input_zip.read(name) for name in input_zip.namelist()} def extract_all(input_zip): return {entry: extract_zip(entry) for entry in ZipFile(input_zip).namelist() if is_zipfile(entry)} 
+3
source

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') 
+1
source

Source: https://habr.com/ru/post/1416201/


All Articles