Can mmap and gzip collaborate?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 03:56:11

Well, not the way you want.

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

mmap() is a system call for mapping disk blocks into RAM almost as if you were adding swap.

You can't map the uncompressed data into RAM with mmap() as it is not on the disk.

You can do easilly. Indeed the gzip module gets as optional argument a file-like object.

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 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()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!