How to decompress zip files across a Windows folder in Python
Question: I have a large folder having 900+ sub-folders, each of which has another
folder inside it which in turn has a zipped file. Its like -  
-MyFolder  
\-----MySubfolder  
\---------MySubSubfolder  
\-------------MyFile.zip  
How can I decompress all the zipped files in their respective folder OR in a
separate folder elsewhere in Windows using Python?

Any help would be great!!


Answer: You could try something like:

    
    
    import zipfile,os;
    def unzip(source_filename, dest_dir):
        with zipfile.ZipFile(source_filename) as zf:
            for member in zf.infolist():
                extract_allowed = True;
                path = dest_dir;
                words = member.filename.split('/');
                for word in words:
                    if (word == '..'):
                        extract_allowed = False;
                        break;
                if (extract_allowed == True):
                    zf.extract(member, dest_dir);
    def unzipFiles(dest_dir):
        for file in os.listdir(dest_dir):
            if (os.path.isdir(dest_dir + '/' + file)):
                return unzipFiles(dest_dir + '/' + file);
            if file.endswith(".zip"):
                print 'Found file: "' + file + '" in "' + dest_dir + '" - extracting';
                unzip(dest_dir + '/' + file, dest_dir + '/');
    unzipFiles('./MyFolder');
    

