简体   繁体   English

如何修改压缩的tar文件中的文件?

[英]How can I modify a file in a gzipped tar file?

I want to write a (preferably python) script to modify the content of one file in a gzipped tar file. 我想编写一个(最好是python)脚本来修改压缩的tar文件中一个文件的内容。 The script must run on FreeBSD 6+. 该脚本必须在FreeBSD 6+上运行。

Basically, I need to: 基本上,我需要:

  • open the tar file 打开tar文件
  • if the tar file has _MY_FILE_ in it: 如果tar文件中包含_MY_FILE_:
    • if _MY_FILE_ has a line matching /RE/ in it: 如果_MY_FILE_中包含与/ RE /匹配的行:
    • insert LINE after the matching line 在匹配行之后插入LINE
  • rewrite the content into the tar file, preserving all metadata except the file size 将内容重写为tar文件,保留除文件大小以外的所有元数据

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. Python的tarfile模块似乎无法在压缩时打开tar文件以进行读/写访问,这在一定程度上是有道理的。 However, I can't find a way to copy the tar file with modifications, either. 但是,我也找不到复制修改后的tar文件的方法。

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. 不要将tar文件视为可以读取/写入的数据库-事实并非如此。 A tar file is a concatenation of files. tar文件是文件的串联。 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. 您要做的是按文件处理tarball文件,将文件(带有修改的文件)复制到新的tarball中。 The Python tarfile module should make this easy to do. Python tarfile模块应该使此操作变得容易。 You should be able to retain the attributes by copying them from the old TarInfo object to the new one. 通过将属性从旧的TarInfo对象复制到新的属性,您应该能够保留这些属性。

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. 使用python tarfile打开tarfile,将其重命名。
  • Create a duplicate empty tar for the original file name 为原始文件名创建一个重复的空tar
  • 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.USTAR_FORMAT POSIX.1-1988(ustar)格式。 tarfile.GNU_FORMAT GNU tar format. tarfile.GNU_FORMAT GNU tar格式。 tarfile.PAX_FORMAT POSIX.1-2001 (pax) format. tarfile.PAX_FORMAT POSIX.1-2001(pax)格式。 tarfile.DEFAULT_FORMAT tarfile.DEFAULT_FORMAT

http://docs.python.org/library/tarfile.html http://docs.python.org/library/tarfile.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM