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)
| 2359 | self.addfile(tarinfo) |
| 2360 | |
| 2361 | def addfile(self, tarinfo, fileobj=None): |
| 2362 | """Add the TarInfo object 'tarinfo' to the archive. If 'tarinfo' represents |
| 2363 | a non zero-size regular file, the 'fileobj' argument should be a binary file, |
| 2364 | and tarinfo.size bytes are read from it and added to the archive. |
| 2365 | You can create TarInfo objects directly, or by using gettarinfo(). |
| 2366 | """ |
| 2367 | self._check("awx") |
| 2368 | |
| 2369 | if fileobj is None and tarinfo.isreg() and tarinfo.size != 0: |
| 2370 | raise ValueError("fileobj not provided for non zero-size regular file") |
| 2371 | |
| 2372 | tarinfo = copy.copy(tarinfo) |
| 2373 | |
| 2374 | buf = tarinfo.tobuf(self.format, self.encoding, self.errors) |
| 2375 | self.fileobj.write(buf) |
| 2376 | self.offset += len(buf) |
| 2377 | bufsize=self.copybufsize |
| 2378 | # If there's data to follow, append it. |
| 2379 | if fileobj is not None: |
| 2380 | copyfileobj(fileobj, self.fileobj, tarinfo.size, bufsize=bufsize) |
| 2381 | blocks, remainder = divmod(tarinfo.size, BLOCKSIZE) |
| 2382 | if remainder > 0: |
| 2383 | self.fileobj.write(NUL * (BLOCKSIZE - remainder)) |
| 2384 | blocks += 1 |
| 2385 | self.offset += blocks * BLOCKSIZE |
| 2386 | |
| 2387 | self.members.append(tarinfo) |
| 2388 | |
| 2389 | def _get_filter_function(self, filter): |
| 2390 | if filter is None: |