compression level with tarfile (w:gz) ?

E

Esmail

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top