def test_addition(self): """Test if regular files are added correctly. For this, we extract all regular files from our sample tar and add them to a new one, which we check afterwards. """ files = [] for tarinfo in self.tar: if tarinfo.isreg(): self.tar.extract(tarinfo, TEMPDIR) files.append(tarinfo.name) buf = StringIO.StringIO() tar = tarfile.open("test.tar", "w", buf) for f in files: path = join(TEMPDIR, f) tarinfo = tar.gettarinfo(path) tarinfo.name = f tar.addfile(tarinfo, file(path, "rb")) tar.close() buf.seek(0) tar = tarfile.open("test.tar", "r", buf) for tarinfo in tar: data1 = file(join(TEMPDIR, tarinfo.name), "rb").read() data2 = tar.extractfile(tarinfo).read() self.assert_(data1 == data2) tar.close()
def setUp(self): os.mkdir(TEMPDIR) self.tar = tarfile.open(SAMPLETAR) self.tar.errorlevel = 1