5

say we have a tempfile to which we could write some output. but for some strange reason we want it to be compressed by some of archiving techniques, tar for instance,

by tempfile I mean the one which has not visiable name created by tempfile.TemporaryFile(). any ideas? my first impression includes some tweeking and class inheritance.. are there any simpler ways?

user3663978
  • 121
  • 2
  • 5
  • 1
    search doc for `tempfile`, `tarfile`, and you will find many well working options even on standard Python library. In your question it is not clear, if you ask for using `tempfile.TemporaryFile()` or you want prevent using it. – Jan Vlcinsky May 27 '14 at 07:59
  • true it is not clear enough, I mainly want to create compressed (hash-a'like) content and store it temporarily inside a file created by tempfile.TemporaryFile(), :) – user3663978 May 27 '14 at 08:14
  • this content could be a source code, got by introspecting actual working a archiving program using inspect method, – user3663978 May 27 '14 at 08:15
  • btw - `tar` is not compressing on its own. But with options like "tar.gz", "tar.bz2" it serves very well. – Jan Vlcinsky May 27 '14 at 08:19

1 Answers1

4

The fileobj argument of tarfile.open serves exactly this purpose: you can pass any file-like object that support writting to it and tarfile will happily use that instead of trying to reach out to the filesystem. Note that such files should be opened in binary mode and not in text mode (which tempfile.TemporaryFile does by default).

Similarly, the first argument to zipfile.ZipFile can be a file-like object.

So you could do:

with tempfile.TemporaryFile(suffix='.tar.gz') as f:
    with tarfile.open(fileobj=f, mode='w:gz') as tar:
        tar.add(…)

    f.flush()
    f.seek(0)
    print(f.read())

Or

with tempfile.NamedTemporaryFile('wb', suffix='.tar.gz', delete=False) as f:
    with tarfile.open(fileobj=f, mode='w:gz') as tar:
        tar.add(…)

to keep f.name on your filesystem and explore its content later.

301_Moved_Permanently
  • 4,007
  • 14
  • 28