mmap和gzip可以合作吗? [英] Can mmap and gzip collaborate?

查看:98
本文介绍了mmap和gzip可以合作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何将mmap与gzip压缩文件一起使用.那有可能吗?

I'm trying to figure how to use mmap with a gzip compressed file. Is that even 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)

输出数据已压缩.

推荐答案

您可以轻松进行.的确,gzip模块作为一个可选参数来获得一个类似文件的对象.

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()

对于tarfile模块也是如此:

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()

这篇关于mmap和gzip可以合作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆