Search code examples
pythonprogressstatusbarprogressdialogzip

python unzip files and provide update to wxPython status bar


I have several large zip file that contain a dir structure that I must maintain. Currently to unzip them I am using

    zip = zipfile.ZipFile(self.fileName)        
    zip.extractall(self.destination)
    zip.close()

The problem is that these process can take upwards of 3-5 minutes and I have no feedback that they are still working. What I would like to do is output the name of the file currently being unziped to the status bar of my gui. What I have in mind is something like

    zip = zipfile.ZipFile(self.fileName)
    zipNameList = zipfile.namelist(self.fileName)
    for item in zipNameList:
        self.SetStatusText("Unzipping" + str(item))
        zip.extract(item)
    zip.close()

The problem with this is that it does not create the correct dir structure. I am not sure that this is even the best way to go about it.

I was also looking into using wx.progressdialog but could not come up with a way to have it show progress of the zip.extractall(filename).


Solution

  • I got it to an acceptable solution - Though I think I would prefer it thread it eventually.

    def unzipItem(self, fileName, destination)
        print "--unzipItem--"
        zip = zipfile.ZipFile(fileName)
        nameList = zip.namelist()
    
        #get the amount of files in the file to properly size the progress bar
        fileCount = 0
        for item in nameList:
            fileCount += 1
    
        #Built progress dialog
        dlg = wx.ProgressDialog("Unziping files",
                               "An informative message",
                               fileCount,
                               parent = self,
                               )
    
        keepGoing = True
        count = 0
    
        for item in nameList:
            count += 1
            dir,file = os.path.split(item)
            print "unzip " + file
    
            #update status bar
            self.SetStatusText("Unziping " + str(item))
            #update progress dialog
            (keepGoing, skip) = dlg.Update(count, file)
            zip.extract(item,destination)
    
        zip.close()