Add a file to a compressed tarfile

Martin Franklin mfranklin1 at
Fri Nov 5 14:26:22 CET 2004

On Sat, 06 Nov 2004 00:13:16 +1100, Dennis Hotson <djdennie69 at>  

> Hi,
> I'm trying to write a function that adds a file-like-object to a
> compressed tarfile...  eg ".tar.gz" or ".tar.bz2"
> I've had a look at the tarfile module but the append mode doesn't support
> compressed tarfiles... :(
> Any thoughts on what I can do to get around this?
> Cheers!

 From the tarfile docs in python 2.3:-

New in version 2.3.

The tarfile module makes it possible to read and create tar archives. Some  
facts and figures:

reads and writes gzip and bzip2 compressed archives.
creates POSIX 1003.1-1990 compliant or GNU tar compatible archives.
reads GNU tar extensions longname, longlink and sparse.
stores pathnames of unlimited length using GNU tar extensions.
handles directories, regular files, hardlinks, symbolic links, fifos,  
character devices and block devices and is able to acquire and restore  
file information like timestamp, access permissions and owner.
can handle tape devices.

open(	[name[, mode [, fileobj[, bufsize]]]])	
Return a TarFile object for the pathname name. For detailed information on  
TarFile objects, see TarFile Objects (section 7.19.1).

mode has to be a string of the form 'filemode[:compression]', it defaults  
to 'r'. Here is a full list of mode combinations:

mode 	action 	
'r'	Open for reading with transparent compression (recommended).	
'r:'	Open for reading exclusively without compression.	
'r:gz'	Open for reading with gzip compression.	
'r:bz2'	Open for reading with bzip2 compression.	
'a' or 'a:'	Open for appending with no compression.	
'w' or 'w:'	Open for uncompressed writing.	
'w:gz'	Open for gzip compressed writing.	
'w:bz2'	Open for bzip2 compressed writing.	

Note that 'a:gz' or 'a:bz2' is not possible. If mode is not suitable to  
open a certain (compressed) file for reading, ReadError is raised. Use  
mode 'r' to avoid this. If a compression method is not supported,  
CompressionError is raised.

If fileobj is specified, it is used as an alternative to a file object  
opened for name.


Using M2, Opera's revolutionary e-mail client:

More information about the Python-list mailing list