tags:

views:

37

answers:

3

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.

AFAICS 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.

+1  A: 

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 ?

Seth
+1  A: 

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.

ire_and_curses
+1  A: 

Ok, I have found a better solution.

f = t.fileobj.__class__(newfn, 'w')
Michał Górny