language: Python (python 2.7.3)
date: 348 days 5 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''    
    I want a function which returns:
    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>))
    
    For input: 
        file.zip:
            outer\
            outer\inner1.zip:
                    file1.txt
                    file2.txt
            outer\inner2.zip:
                    file3.txt
                    file4.txt
'''
 
from zipfile import ZipFile, is_zipfile
from StringIO import StringIO
 
def extract_zip(input_zip):
    return StringIO(ZipFile(StringIO(input_zip)).extractall())
 
def recursive_zip_contents(input_zip):
    return [extract_zip(this_zip) for this_zip in extract_zip(input_zip) if is_zipfile(this_zip)]
 
def get_filename_list_pairs(input_zip):
    return {(key, (word for word in open(key, 'r')).read()) for key in recursive_zip_contents(input_zip)} 
 
if __name__ == "__main__":
    print get_filename_list_pairs('file.zip')
Traceback (most recent call last):
File "unzip_all.py", line 32, in <module>
print get_filename_list_pairs('file.zip')
File "unzip_all.py", line 29, in get_filename_list_pairs
return {(key, (word for word in open(key, 'r')).read()) for key in recursive_zip_contents(input_zip)}
File "unzip_all.py", line 26, in recursive_zip_contents
return [extract_zip(this_zip) for this_zip in extract_zip(input_zip) if is_zipfile(this_zip)]
File "unzip_all.py", line 23, in extract_zip
return StringIO(ZipFile(StringIO(input_zip)).extractall())
File "C:\Python27\lib\zipfile.py", line 714, in __init__
self._GetContents()
File "C:\Python27\lib\zipfile.py", line 748, in _GetContents
self._RealGetContents()
File "C:\Python27\lib\zipfile.py", line 763, in _RealGetContents
raise BadZipfile, "File is not a zip file"
zipfile.BadZipfile: File is not a zip file