Create tar file in string using python

I need to create a tar file, but as a string in memory, and not as an actual file. As input, I mean one file name and a string containing the associated content. I am looking for a python lib that I can use, and avoid the need to create my own.


A bit more work has been found for these functions , but using a passive memory object seems a bit ... inelegant. And forcing it to accept input from strings, it looks even bigger ... inelegant. OTOH it works. I suppose, since most of them are new to me. Does anyone see any errors in it?

+3
source share
1 answer

tarfile cStringIO:

c = cStringIO.StringIO()
t = tarfile.open(mode='w', fileobj=c)
# here: do your work on t, then...:
s = c.getvalue()   # extract the bytestring you need
+15

Source: https://habr.com/ru/post/1762209/


All Articles