compression level with tarfile (w:gz) ?

Esmail ebonak at hotmail.com
Mon Aug 10 08:50:21 EDT 2009


Hello,

I was wondering if it possible to specify a compression level when I
tar/gzip a file in Python using the tarfile module. I would like to
specify the highest (9) compression level for gzip.

Ideally:

    t = tarfile.open(tar_file_name+'.tar.gz', mode='w:gz:9')

When I create a simple tar and then gzip it 'manually' with compression
level 9, I get a smaller archive than when I have this code execute with
the w:gz option.

Is the only way to accomplish the higher rate to create a tar file
and then use a different module to gzip it (assuming I can specify
the compression level there)?

Thanks,
Esmail

-----------------
My current code:
-----------------

def tar_it_up(target_dir_name, tar_file_name=None):
     '''
     tar up target_dir_name directory and create a
     tar/zip file with base name tar_file_name

     appends a date/timestamp to tar_file_name
     '''

     time_string = time.strftime("_%b_%d_%Y_%a_%H_%M")

     if tar_file_name is None:
         tar_file_name = target_dir_name

     tar_file_name += time_string

     print ('Creating archive of %s ...' % target_dir_name),

     t = tarfile.open(tar_file_name+'.tar.gz', mode='w:gz')
#   t = tarfile.open(tar_file_name+'.tar', mode='w')
     t.add(target_dir_name)
     t.close()

     print ('saved to %s.tar.gz' % tar_file_name)




More information about the Python-list mailing list