3 回答

幕布斯6054654
TA貢獻1876條經驗 獲得超7個贊
您將更高興創建一個臨時文件。這樣可以節省大量內存。當您同時有一個或兩個以上的用戶時,您會發現節省內存非常重要。
但是,您可以寫入StringIO對象。
>>> import zipfile
>>> import StringIO
>>> buffer= StringIO.StringIO()
>>> z= zipfile.ZipFile( buffer, "w" )
>>> z.write( "idletest" )
>>> z.close()
>>> len(buffer.getvalue())
778
“緩沖區”對象類似于具有778字節ZIP存檔的文件。

翻過高山走不出你
TA貢獻1875條經驗 獲得超3個贊
為什么不制作tar文件呢?像這樣:
def downloadLogs(req, dir):
response = HttpResponse(content_type='application/x-gzip')
response['Content-Disposition'] = 'attachment; filename=download.tar.gz'
tarred = tarfile.open(fileobj=response, mode='w:gz')
tarred.add(dir)
tarred.close()
return response
添加回答
舉報
0/150
提交
取消