The Python tarfile
library does not detect a broken tar.
user@host$ wc -c good.tar
143360 good.tar
user@host$ head -c 130000 good.tar > cut.tar
user@host$ tar -tf cut.tar
...
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
Very nice, the command line tool recognizes an unexpected EOF.
user@host$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
>>> import tarfile
>>> tar=tarfile.open('cut.tar')
>>> tar.extractall()
Not nice. The Python library decodes the file, but raises no exception.
How to detect unexpected EOF with the Python library? I want to avoid the subprocess
module.
The parameter errorlevel
does not help. I tried errorlevel=1 and errorlevel=2.
I wrote a work around. It works with my tar files. I guess it supports not all types of objects which can be stored in a tar file.
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import os
import tarfile
class TarfileWhichRaisesOnEOF(tarfile.TarFile):
def extractall(self, path=".", members=None):
super(TarfileWhichRaisesOnEOF, self).extractall(path, members)
if members is None:
members = self
for tarinfo in members:
if not tarinfo.isfile():
continue
file=os.path.join(path, tarinfo.name)
size_real=os.path.getsize(file)
if size_real!=tarinfo.size:
raise tarfile.ExtractError('Extracting %s: Size does not match. According to tarinfo %s and on disk %s' % (
tarinfo, tarinfo.size, size_real))
This has been fixed in Python 3 -- an OSError
is raised regardless of the errorlevel
setting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With