language: Python (python 2.7.3)
date: 351 days 15 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
 
def unarchive_all(input_zip):
    a=[]
    z = ZipFile(input_zip);
    for member in z.namelist():
        if is_zipfile(member):
            sub_z = ZipFile(file=z.open(member))
            for filename in sub_z.namelist():
                a.append(filename) # just for testing
    return a
 
if __name__ == "__main__":
    print unarchive_all('file.zip')
Traceback (most recent call last):
File "unzip_all.py", line 32, in <module>
print unarchive_all('file.zip')
File "unzip_all.py", line 26, in unarchive_all
sub_z = ZipFile(file=z.open(member))
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 761, in _RealGetContents
raise BadZipfile("File is not a zip file")
zipfile.BadZipfile: File is not a zip file