开发者

tarfile: determine compression of an open tarball

I am on working on a Python script which is supposed to process a tarball and output new one, trying to keep the format of the original. Thus, I am looking for a way to lookup the compression method used in an open tarball to open the new one with same compression.

AF开发者_开发百科AICS TarFile class doesn't provide any public interface to get the needed information directly. And I would like to avoid reading the file independently of the tarfile module.

I am currently considering looking up the class of the underlying file object (t.fileobj.__class__) or trying to open the input file in all possible modes and choosing the correct format basing on which one succeeds.


Ok, I have found a better solution.

f = t.fileobj.__class__(newfn, 'w')


Tar doesn't compress, it concatenates (which is why TarFile won't tell you what compression method is used, because there isn't one).

Are you trying to find out if it's a tar.gz, tar.bz2, or tar.Z ?


When you open the tarfile, you can choose the mode. From the docs:

If mode is not suitable to open a certain (compressed) file for reading, ReadError is raised.

So why not try opening the file as a .gz, .bz2 etc., catching the exception each time? The one that opens without an exception tells you the type of compression you want to replicate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜