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. from zipfile import ZipFile, is_zipfile
  20. from StringIO import StringIO
  21. from shutil import rmtree
  22. from os import path
  23. import tempfile
  24.  
  25. def unzip_all(input_zip, name, file_contents_pair=dict()):
  26. if is_zipfile(input_zip):
  27. contents=StringIO(ZipFile(StringIO(input_zip)).extractall())
  28. file_contents_pair.update(name, [word for word in contents])
  29. return file_contents_pair
  30.  
  31. def unzip_from(input_zip):
  32. if not is_zipfile(input_zip): return dict()
  33. file_contents_pairs = dict()
  34. tmpdir = tempfile.mkdtemp()
  35. input_zip=ZipFile(input_zip)
  36. input_zip.extractall(path=tmpdir)
  37. i=False
  38. for member in input_zip.namelist():
  39. if i: # Skipping first file
  40. file_contents_pairs.update(unzip_all(ZipFile(file=ZipFile(path.join(tmpdir, member))), member))
  41. i = True
  42. rmtree(tmpdir)
  43. return file_contents_pairs
  44.  
  45. if __name__ == '__main__':
  46. print unzip_from('file.zip')
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty