Can mmap and gzip work together?

I am trying to figure out how to use mmap with a gzip compressed file. Is it possible?

  import mmap
 import os
 import gzip

 filename = r'C: \ temp \ data.gz '

 file = gzip.open (filename, "rb +")
 size = os.path.getsize (filename)

 file = mmap.mmap (file.fileno (), size)

 print file.read (8)

The output is compressed.

+7
source share
2 answers

Well, not the way you want.

mmap () can be used to access a gzipped file if compressed data is what you want.

mmap () - a system call to display disk blocks in RAM is almost the same as if you were adding a swap.

You cannot match uncompressed data in RAM with mmap () since it is not on disk.

+12
source

You can do easilly. Indeed, the gzip module receives a file object as an optional argument.

import mmap import gzip filename = "a.gz" handle = open(filename, "rb") mapped = mmap.mmap(handle.fileno(), 0, access=mmap.ACCESS_READ) gzfile = gzip.GzipFile(mode="r", fileobj=mapped) print gzfile.read() 

The same applies to the tarfile module:

 import sys import mmap import tarfile f = open(sys.argv[1], 'rb') fo = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) tf = tarfile.open(mode='r:gz', fileobj=fo) print tf.getnames() 
+12
source

All Articles