The absolutely simplest solution is to (temporarily?) chmod
the file before adding it.
As you already discovered, 'w'
mode will overwrite any existing file. To update an existing archive, you want to open the file in 'a'
mode instead.
However, unfortunately, the tarfile
library does not support opening a gz
file in a
mode. You can do this if you have an uncompressed file:
import tarfile
def chmodx(tarinfo):
tarinfo.mode = int('0755', base=8)
return tarinfo
with tarfile.open('test.tar', 'a'):
f.add('logger.sh' , arcname='/tmp/logger.sh', filter=chmodx)
The nature of gz
compression is such that you probably simply want to uncompress the file, update it, and then recompress, rather than do it all in one go. Ideally, you'd want to do this in memory, but that's problematic if the uncompressed tar file is large.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…