How to determine if data is valid tar file without a file?
python, tar, tarfile
Solution
Say your uploaded data is contained in string `data`.
from tarfile import TarFile, TarError
from StringIO import StringIO
sio = StringIO(data)
try:
tf = TarFile(fileobj=sio)
# process the file....
except TarError:
print "Not a tar file"
There are additional complexities such as handling different tar file formats and compression. More info is available in the tarfile documentation.
Problem
My upload form expects a tar file and I want to check whether the uploaded data is valid. The tarfile module supports `is_tarfile()`, but expects a filename - I don't want to waste resources writing the file to disk just to check if it is valid. Is there a way to check the data is a valid tar file without writing to disk, using standard Python libraries?