tarfile.open(mode='w:gz'|'w|gz'|..., fileobj=StringIO()) fails.

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 26 20:17:04 EDT 2008


En Mon, 26 May 2008 17:44:28 -0300, sebastian.noack at googlemail.com  
<sebastian.noack at googlemail.com> escribió:

> is there a way to or at least a reason why I can not use tarfile to
> create a gzip or bunzip2 compressed archive in the memory?
>
> You might might wanna answer "use StringIO" but this isn't such easy
> as it seems to be. ;) I am using Python 2.5.2, by the way. I think
> this is a bug in at least in this version of python, but maybe
> StringIO isn't just file-like enough for this "korky" tarfile module.
> But this would conflict with its documentation.

> def create_tar_file(filenames, fileobj, mode, result_cb=lambda f:
> None):
> 	tar_file = tarfile.open(mode=mode, fileobj=fileobj)
> 	for f in filenames:
> 		tar_file.add(f)
> 	result = result_cb(fileobj)
> 	tar_file.close()
> 	return result

It's not a bug, you must extract the StringIO contents *after* closing  
tar_file, else you won't get the last blocks pending to be written.

-- 
Gabriel Genellina




More information about the Python-list mailing list