有没有一种方法可以对文件夹进行tarred,并获得tarred流而不是tared文件? 我尝试过使用tar模块,但它直接返回tared文件。在
with tarfile.open("zipped.tar",'w|') as tar:
for base_root, subFolders, files in os.walk('test'):
for j in files:
filepath = os.path.join(base_root,j)
if os.path.isfile(filepath):
with open(filepath, 'rb') as file:
size = os.stat(filepath).st_size
info = tarfile.TarInfo()
info.size = size
info.name = filepath
if(size <= chunck_size):
data = file.read(info.size)
fobj = StringIO.StringIO(data)
tar.addfile(info, fobj)
else:
data = ""
while True:
temp_data = file.read(chunck_size)
if temp_data == '':
break
data = data + temp_data
fobj = StringIO.StringIO(data)
tar.addfile(info, fobj)
根据the documentation,
open
可以采用fileobj
参数:所以你可以写这个,然后使用缓冲区对象。在
相关问题 更多 >
编程相关推荐