Add the TarInfo object 'tarinfo' to the archive. If 'tarinfo' represents a non zero-size regular file, the 'fileobj' argument should be a binary file, and tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects directly, or by usi
(self, tarinfo, fileobj=None)
| 2340 | self.addfile(tarinfo) |
| 2341 | |
| 2342 | def addfile(self, tarinfo, fileobj=None): |
| 2343 | """Add the TarInfo object 'tarinfo' to the archive. If 'tarinfo' represents |
| 2344 | a non zero-size regular file, the 'fileobj' argument should be a binary file, |
| 2345 | and tarinfo.size bytes are read from it and added to the archive. |
| 2346 | You can create TarInfo objects directly, or by using gettarinfo(). |
| 2347 | """ |
| 2348 | self._check("awx") |
| 2349 | |
| 2350 | if fileobj is None and tarinfo.isreg() and tarinfo.size != 0: |
| 2351 | raise ValueError("fileobj not provided for non zero-size regular file") |
| 2352 | |
| 2353 | tarinfo = copy.copy(tarinfo) |
| 2354 | |
| 2355 | buf = tarinfo.tobuf(self.format, self.encoding, self.errors) |
| 2356 | self.fileobj.write(buf) |
| 2357 | self.offset += len(buf) |
| 2358 | bufsize=self.copybufsize |
| 2359 | # If there's data to follow, append it. |
| 2360 | if fileobj is not None: |
| 2361 | copyfileobj(fileobj, self.fileobj, tarinfo.size, bufsize=bufsize) |
| 2362 | blocks, remainder = divmod(tarinfo.size, BLOCKSIZE) |
| 2363 | if remainder > 0: |
| 2364 | self.fileobj.write(NUL * (BLOCKSIZE - remainder)) |
| 2365 | blocks += 1 |
| 2366 | self.offset += blocks * BLOCKSIZE |
| 2367 | |
| 2368 | self.members.append(tarinfo) |
| 2369 | |
| 2370 | def _get_filter_function(self, filter): |
| 2371 | if filter is None: |