为什么 tarfile 模块不允许压缩附加? [英] Why tarfile module does not allow compressed appending?

查看:29
本文介绍了为什么 tarfile 模块不允许压缩附加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有直接的方法可以附加到压缩的 tar 存档.正如文档所述:

There is no straight way to append to a compressed tar archive. As the documentation states:

请注意,'a:gz''a:bz2''a:xz' 是不可能的.

Note that 'a:gz', 'a:bz2' or 'a:xz' is not possible.

作为一种解决方法,您可以使用未压缩的追加模式 'a' 然后自己处理压缩和解压缩,或者您可以自己处理追加并使用压缩的读/写模式重新创建tar 存档.

As a workaround you can either use the uncompressed append mode 'a' and then handle the compression and decompression yourself, or you can handle the appending yourself and use the compressed read/write modes to recreate the tar archive.

我的问题是:为什么要这么复杂?您有什么理由可以考虑为什么开发人员会决定不在代码中包含压缩附加模式?

My question is: Why does it have to be this complicated? Is there any reason you can think of why the developers would decide to not include the compressed appending mode in the code?

推荐答案

因为它很难,特别是如果您希望它在附加小文件时仍能很好地压缩.

Because it's hard, especially if you want it to still compress well when appending small files.

您可以查看 gzlog 以了解如何有效地将小字符串附加到 gzip 文件中.它将未压缩的数据附加到阈值,此时未压缩的数据被压缩.每次追加后,您都有一个有效的 gzip 文件.

You can look at gzlog for how to efficiently append small strings to a gzip file. It appends them uncompressed until a threshold is reached, at which point the uncompressed data is compressed. After each append you have a valid gzip file.

要使用 tar 文件执行此操作,您还需要跟踪未压缩的 .tar 流中的终止块并覆盖这些终止块,每次附加新的终止块.

To do this with a tar file, you would also need to keep track of the terminating blocks in the uncompressed .tar stream and write over those, appending new terminating blocks each time.

这是可以做到的,但需要特别注意每种压缩数据格式才能正确执行.如果做错了,如果经常使用,结果在时间和压缩率上都会非常低效.

It could be done, but it would require very special attention to each compressed data format to do it right. If done wrong, and if used frequently, the result would be horribly inefficient both in time and compression ratio.

这篇关于为什么 tarfile 模块不允许压缩附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆