Created
January 9, 2019 21:36
-
-
Save stdevPavelmc/61301dce73431c0929e7289211d0e76c to your computer and use it in GitHub Desktop.
Python3 tarfile extraction with a pprogress callback function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: [email protected] | |
# 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