Can mmap and gzip collaborate?

后端 未结 2 1821
轮回少年
轮回少年 2021-02-13 17:49

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          


        
2条回答
  •  借酒劲吻你
    2021-02-13 18:17

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

提交回复
热议问题