tar_stream.py 1.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/python3

import tarfile
import os
import threading

class TarWriter:

    def __init__(self, fileobj):
        self.tarfile = tarfile.open(fileobj=fileobj,
                                    mode='w|',
                                    format=tarfile.PAX_FORMAT)

14
15
    def add(self, path, arcname=None):
        self.tarfile.add(path, arcname=arcname, recursive=False)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        # Keep memory consumption down
        self.tarfile.members = []
        self.tarfile.inodes = {}
        pass

    def close(self):
        self.tarfile.close()
        self.tarfile = None
        
class TarReader:

    def __init__(self, fileobj):
        self.fileobj = fileobj
        self.tarfile = None
        pass

    def __iter__(self):
        if self.tarfile == None:
            self.tarfile = tarfile.open(fileobj=self.fileobj,
                                        mode='r|')
            pass
        for e in self.tarfile:
            yield e
            # Keep memory consumption down
            self.tarfile.members = []
            pass
        pass
        
    def close(self):
        self.tarfile.close()
        self.tarfile = None