使用 python 在字符串中创建 tar 文件

发布于 2024-09-16 04:25:11 字数 295 浏览 2 评论 0原文

我需要生成一个 tar 文件,但作为内存中的字符串而不是实际文件。我输入的是一个文件名和一个包含相关内容的字符串。我正在寻找一个我可以使用的 python 库,并且避免自己扮演角色。


发现了更多的工作这些函数,但使用内存蒸汽对象似乎有点.. . 不优雅。让它接受来自字符串的输入看起来更……不优雅。哦,它有效。我想,因为其中大部分对我来说都是新的。有人看到其中有任何错误吗?

I need to generate a tar file but as a string in memory rather than as an actual file. What I have as input is a single filename and a string containing the assosiated contents. I'm looking for a python lib I can use and avoid having to role my own.


A little more work found these functions but using a memory steam object seems a little... inelegant. And making it accept input from strings looks like even more... inelegant. OTOH it works. I assume, as most of it is new to me. Anyone see any bugs in it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2024-09-23 04:25:11

tarfilecStringIO

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

Use tarfile in conjunction with 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文