Skip to content

Instantly share code, notes, and snippets.

@stdevPavelmc
Created January 9, 2019 21:36
Show Gist options
  • Save stdevPavelmc/61301dce73431c0929e7289211d0e76c to your computer and use it in GitHub Desktop.
Save stdevPavelmc/61301dce73431c0929e7289211d0e76c to your computer and use it in GitHub Desktop.
Python3 tarfile extraction with a pprogress callback function
#!/usr/bin/env python3
# A sample code to have a tarfile extraction handle a progress callback
# tarfile must handle gz, bz2 & xz files transparently
# Author: pavelmc@gmail.com
# Title: tar extraction with progress bar
import io
import os
import tarfile
class ProgressFileObject(io.FileIO):
"""Overide the fileio object to have a callback on progress"""
def __init__(self, path, *args, **kwargs):
self._total_size = os.path.getsize(path)
# callback will be a function passed on progressfn
if kwargs["progressfn"]:
self.progfn = kwargs["progressfn"]
# must remove the progressfn if present from the kwargs to make fileio happy
kwargs.pop("progressfn")
io.FileIO.__init__(self, path, *args, **kwargs)
def read(self, size):
"""Each time a chunk in read call the progress function if there"""
if self.progfn:
# must calc and call the progress callback function
progress = self.tell() / self._total_size
self.progfn(progress)
return io.FileIO.read(self, size)
def prog(prog):
print("Extraction progress is {:0.2%}".format(prog))
file = "./your_big_file.tar.gz"
tar = tarfile.open(fileobj=ProgressFileObject(file, progressfn=prog))
tar.extractall()
tar.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment