为了避免额外的数据拷贝,想了半天怎么压缩到socket,而且发现了issue
import tarfile
copybufsize = 1024
class Fo:
def __init__(self):
self.fileobj = open('myshm.tar', 'wb')
def read(self,b):
print('read')
return self.fileobj.read(b)
def tell(self):
print('tell',self.fileobj.tell())
return self.fileobj.tell()
def write(self,write):
r =self.fileobj.write(write)
print('write',r)
return r
def close(self):
print('Fo close')
return self.fileobj.close()
fo = Fo()
t = tarfile.open('myshm.tar', 'w|', copybufsize=copybufsize,fileobj=fo,bufsize=copybufsize)
tarinfo = tarfile.TarInfo('aaashm')
tarinfo.size = 1 * 1024 ** 2
class Fi:
def __init__(self):
self.f = open('myshm', 'rb')
def read(self, bs):
print('read src',bs)
return self.f.read(bs)
def close(self):
print('Fi close')
f = Fi()
t.addfile(tarinfo,)
t.close()
t.fileobj.close()
fo.close()
f.close()
原来打开模式就可以支持。。。
'w|'方式打开是stream方式的压缩写
'r|'方式打开是stream方式的解压读
def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs):
"""Open a tar archive for reading, writing or appending. Return
an appropriate TarFile class.
mode:
'r' or 'r:*' open for reading with transparent compression
'r:' open for reading exclusively uncompressed
'r:gz' open for reading with gzip compression
'r:bz2' open for reading with bzip2 compression
'r:xz' open for reading with lzma compression
'a' or 'a:' open for appending, creating the file if necessary
'w' or 'w:' open for writing without compression
'w:gz' open for writing with gzip compression
'w:bz2' open for writing with bzip2 compression
'w:xz' open for writing with lzma compression
'x' or 'x:' create a tarfile exclusively without compression, raise
an exception if the file is already created
'x:gz' create a gzip compressed tarfile, raise an exception
if the file is already created
'x:bz2' create a bzip2 compressed tarfile, raise an exception
if the file is already created
'x:xz' create an lzma compressed tarfile, raise an exception
if the file is already created
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
'r|bz2' open a bzip2 compressed stream of tar blocks
'r|xz' open an lzma compressed stream of tar blocks
'w|' open an uncompressed stream for writing
'w|gz' open a gzip compressed stream for writing
'w|bz2' open a bzip2 compressed stream for writing
'w|xz' open an lzma compressed stream for writing
"""