On Mon, Aug 10, 2009 at 8:50 AM, Esmail<ebo...@hotmail.com> wrote: > 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. >
Looking at the tarfile docs, it seems that there are tarfile.gzopen and tarfile.bz2open functions that have a compresslevel parameter that defaults to 9. You can't append using those functions but you can read and write. > 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) > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list