问题
There is no straight way to append to a compressed tar archive. As the documentation states:
Note that
'a:gz'
,'a:bz2'
or'a:xz'
is not possible.
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?
回答1:
Because it's hard, especially if you want it to still compress well when appending small files.
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.
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.
来源:https://stackoverflow.com/questions/52276027/why-tarfile-module-does-not-allow-compressed-appending