entzip0r.py


1
#!/usr/bin/python
2
3
import os
4
import fnmatch
5
import zipfile
6
import tempfile
7
8
def gen_find(filepat,top):
9
    for path, dirlist, filelist in os.walk(top):
10
        for name in fnmatch.filter(filelist,filepat):
11
            yield os.path.join(path,name)
12
13
# zz = file object auf ein .zip file
14
def gen_open_files_in_zip(zippath, zz):
15
    myzip = zipfile.ZipFile(zz, 'r')
16
    # inhalt des zipfiles durchgehen:
17
    for f in myzip.infolist():
18
        new_zippath = zippath + '|' + f.filename
19
        if f.filename.endswith('.zip'):
20
            # zwischendurch auf die platte speichern
21
            tmpf = tempfile.TemporaryFile()
22
            tmpf.write(myzip.open(f).read())
23
            # rekursion
24
            temp_gen = gen_open_files_in_zip(new_zippath, tmpf)
25
            for t in temp_gen:
26
                yield t[0], t[1]
27
            tmpf.close() #tempfile wird geloescht
28
        else:
29
            yield new_zippath, myzip.open(f)
30
31
32
# gibt einen iterator ueber tupel aus dateiname und geoeffnetem file object oder ZipExtFile zurueck
33
# auch dateinamen in geschachtelten zipfiles.
34
def gen_open_files(filenames):
35
    for f in filenames:
36
        if f.endswith('.zip'):
37
            z = open(f, 'r')
38
            temp = gen_open_files_in_zip(f, z)
39
            for t in temp:
40
                yield t
41
        else:
42
            yield f, open(f)
43
        
44
if __name__ == '__main__':
45
    allfiles = gen_find('*.*', '/tmp/ziptests')
46
    zip_contents = gen_open_files(allfiles)
47
    for z in zip_contents:
48
        print z[0], 'enthaelt:', z[1].read().strip()