I want to write a (preferably python) script to modify the content of one file in a gzipped tar file. The script must run on FreeBSD 6+.
Basically, I need to:
- open the tar file
- if the tar file has _MY_FILE_ in it:
- if _MY_FILE_ has a line matching /RE/ in it:
- insert LINE after the matching line
- rewrite the content into the tar file, preservin开发者_如何学运维g all metadata except the file size
I'll be repeating this for a lot of files.
Python's tarfile
module doesn't seem to be able to open tar files for read/write access when they're compressed, which makes a certain amount of sense. However, I can't find a way to copy the tar file with modifications, either.
Is there an easy way to do this?
Don't think of a tar file as a database that you can read/write -- it's not. A tar file is a concatenation of files. To modify a file in the middle, you need to rewrite the rest of the file. (for files of a certain size, you might be able to exploit the block padding)
What you want to do is process the tarball file by file, copying files (with modifications) into a new tarball. The Python tarfile module should make this easy to do. You should be able to retain the attributes by copying them from the old TarInfo object to the new one.
I don't see an easy way to remove a single file. You can easily extract one or all, then add any files needed.
I think that the only way is:
- Open the tarfile using python tarfile, rename it.
- Create a duplicate empty tar for the original file name
- Re-add all the files, changing the one you need before re-add
Be sure to reset the correct format when you read it on re-creation
tarfile.USTAR_FORMAT POSIX.1-1988 (ustar) format. tarfile.GNU_FORMAT GNU tar format. tarfile.PAX_FORMAT POSIX.1-2001 (pax) format. tarfile.DEFAULT_FORMAT
http://docs.python.org/library/tarfile.html
精彩评论