def on_progress(filename, position, total_size):
print "%s: %d of %s" %(filename, position, total_size)
class MyFileObject(tarfile.ExFileObject):
def read(self, size, *args):
on_progress(self.name, self.position, self.size)
return tarfile.ExFileObject.read(self, size, *args)
tarfile.TarFile.fileobject = MyFileObject
Check the tarfile.py module code for more details, the standard library is pretty well written and less scary than you would think.
Edit1: removed monkey-patching (or is this still monkey-patching?), it turns out you can set your own file object.
Edit2: To get an overall byte progress, use the the fileobj argument:
total_size = os.path.getsize("a.tgz")
class MyFileObj(file):
def read(self, size):
print "%d of %d" %(self.tell(), total_size)
return file.read(self, size)
tar = tarfile.open(fileobj=MyFileObj("a.tgz"))
tar.extractall()
tar.close()