fork download
  1. '''
  2. I want a function which returns:
  3. dict("file1.txt": list(<contents of file1>),
  4. "file2.txt": list(<contents of file2>),
  5. "file3.txt": list(<contents of file3>),
  6. "file4.txt": list(<contents of file4>))
  7.  
  8. For input:
  9. file.zip:
  10. outer\
  11. outer\inner1.zip:
  12. file1.txt
  13. file2.txt
  14. outer\inner2.zip:
  15. file3.txt
  16. file4.txt
  17. '''
  18.  
  19.  
  20. import os
  21. import shutil
  22. import tempfile
  23. from zipfile import ZipFile
  24.  
  25. def unzip_recursively(parent_archive):
  26. parent_archive = ZipFile(parent_archive)
  27. result = []
  28. tmpdir = tempfile.mkdtemp()
  29. try:
  30. parent_archive.extractall(path=tmpdir)
  31. namelist=parent_archive.namelist()
  32. print namelist
  33. for it in xrange(1, len(namelist)):
  34. sub_z = ZipFile(os.path.join(tmpdir, namelist[it]))
  35. result.extend(sub_z.namelist())
  36. finally:
  37. shutil.rmtree(tmpdir)
  38. return result
  39.  
  40. if __name__ == '__main__':
  41. print unzip_recursively('file.zip')
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty