Tarfile.testtar() similar to zipfile.testzip()?

Zipfile offers a testzip() to check the files of a zip against its CRC (crc32? I think). Tarfile doesn’t have an equivalent method but it does have a TarInfo.chksum property for it’s files. Although I’m not even sure what kind of checksum is it to begin with. So how would I go about achieving something like testtar()?

Well, CRC is a type of checksum but not all checksums are CRC. From my little research, I did not find any way to add CRC to a tar file, you can, however, gzip it, see this answer Does gzip add integrity/crc check to a .tar? - Unix & Linux Stack Exchange for more information.

I’m aware that CRC is a checksum but not all checksums are crc. Zipfile documentation states that the checksum is CRC but tarfile docs do not state what kind of checksum the .chksum attribute returns

Ah, I misunderstood your question sorry. As for the question of what kind that checksum is, I’ve found this:

The chksum field represents the simple sum of all bytes in the header block. Each 8-bit byte in the header is added to an unsigned integer, initialized to zero, the precision of which shall be no less than seventeen bits. When calculating the checksum, the chksum field is treated as if it were filled with spaces (ASCII 32). - GNU tar 1.35: Appendix E Tar Internals


So for checking the integrity of the tar file it’s pretty much useless I believe

  1. The TarInfo.chksum is the header chksum ↩︎


That’s unfortunate, thank you for the help!

As @DerSchinken says there are no checksums in a tar file.

There is a file size in the header that does provide a minimal piece of information you can use to see if a tar file is sane. A valid tar file will be a sequence of headers + contents.
You can check that the size in the header is followed by at least that many bytes of data.
You can repeat that check until the data stream runs out.
At the end you can check that you are at end-of-file.

But there is nothing in the tar format that will allow you to detect any change in the contents.

Often getting a listing of a tar file can be used as the validity check.


I’ll look into that, thanks for the pointers!