pythontartarfile

How to check if a tar file is not empty before unpacking it with Python 3?


I wish to unpack some tar archives but I only want to rpcess non-empty ones. I found some code for gzip archives How to check empty gzip file in Python and also this:

async def is_nonempty_tar_file(self, tarfile):
    with open(tarfile, "rb") as f:
        try:
            file_content = f.read(1)
            return len(file_content) > 1
        except Exception as exc:
            self.logger.error(
                f"Reading tarfile failed for {tarfile}", exc_info=True
            )

All the tar archives both empty and non-empty ones seem to have at least thsi character in them \x1f. SO they all pass the test even if they are empty.

How else can I check this?


Solution

  • OK I found a way using the getmembers() method from tarfile module. I made this method that checks for non empty tarfiles:

     def is_nonempty_tar_file(self, archive):
        with tarfile.open(archive, "r") as tar:
            try:
                file_content = tar.getmembers()
                return len(file_content) > 0
            except Exception as exc:
                print(f"Reading tarfile failed for {archive}")