language: Python (python 2.7.3)
date: 348 days 9 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
33
34
35
36
37
38
39
40
41
'''    
    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
'''
 
 
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()
        print namelist
        for it in xrange(1, len(namelist)):
            sub_z = ZipFile(os.path.join(tmpdir, namelist[it]))
            result.extend(sub_z.namelist())
    finally:
        shutil.rmtree(tmpdir)
    return result
 
if __name__ == '__main__':
    print unzip_recursively('file.zip')
WindowsError: [Error 32] The process cannot access the file because it is being used by another process