memory mapped tar file contents

C

Chris Brooks

Hi,

I would like to read directly from a tar file into memory so I can
manipulate a file (quickly) and write its changes out to another file. I
thought I could do something like:

#!/usr/bin/env python

import tarfile
import mmap

fil = tarfile.open( "out.tar.gz" , "r:gz" )
tarinf = fil.next()
myfils = {}
while tarinf != None:
tarinf = fil.next()
ref = fil.extractfile( tarinf )
myfils[ tarinf.name ] = mmap.mmap( ref.fileno() , 0 )

But the extractfile() function of TarInfo doesn't seem to give me a fileno,
so I can't pass this to mmap.

Thoughts on a way to accomplish this?

Chris
 
A

Aaron Brady

Hi,

I would like to read directly from a tar file into memory so I can
manipulate a file (quickly) and write its changes out to another file.  I
thought I could do something like:

#!/usr/bin/env python

import tarfile
import mmap

fil = tarfile.open( "out.tar.gz" , "r:gz" )
tarinf = fil.next()
myfils = {}
while tarinf != None:
    tarinf = fil.next()
    ref = fil.extractfile( tarinf )
    myfils[ tarinf.name ] = mmap.mmap( ref.fileno() , 0 )

But the extractfile() function of TarInfo doesn't seem to give me a fileno,
so I can't pass this to mmap.

Thoughts on a way to accomplish this?

It appears you have to read the contents of the file into the mmap.
You can create an anonymous map of size tarinfoobj.size, then set
mapobj[:]= fil.extractfile( tarinf ).read( ) . Untested.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top