创建临时压缩文件

时间:2012-08-15 10:26:52

标签: python

我需要创建一个临时文件来发送它,我试过了:

# Create a temporary file --> I think it is ok (file not seen)
temporaryfile = NamedTemporaryFile(delete=False, dir=COMPRESSED_ROOT)

# The path to archive --> It's ok
root_dir = "something"

# Create a compressed file --> It bugs
data = open(f.write(make_archive(f.name, 'zip', root_dir))).read()

# Send the file --> Its ok
response = HttpResponse(data, mimetype='application/zip')
response['Content-Disposition'] = 'attachment; filename="%s"' % unicode(downloadedassignment.name + '.zip')
return response

我根本不知道这是不是很好的方法..

2 个答案:

答案 0 :(得分:23)

我实际上只需要做类似的事情,如果可能的话我想完全避免文件I / O.这就是我想出的:

import tempfile
import zipfile

with tempfile.SpooledTemporaryFile() as tmp:
    with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as archive:
        archive.writestr('something.txt', 'Some Content Here')

    # Reset file pointer
    tmp.seek(0)

    # Write file data to response
    return HttpResponse(tmp.read(), mimetype='application/x-zip-compressed')

它使用SpooledTemporaryFile因此它将保留在内存中,除非它超出内存限制。然后,我将此临时文件设置为要使用的ZipFile的流。传递给writestr的文件名只是文件在存档中的文件名,它与服务器的文件系统没有任何关系。然后,我只需要在seek(0)完成它之后回滚文件指针(ZipFile)并将其转储到响应中。

答案 1 :(得分:11)

首先,您无需创建NamedTemporaryFile即可使用make_archive;您想要的只是要创建的make_archive文件的唯一文件名。

.write不返回文件名

关注该错误:您假设f.write的返回值是您可以打开的文件名;只需查找文件的开头并阅读:

f.write(make_archive(f.name, 'zip', root_dir))
f.seek(0)
data = f.read()

请注意,您需要清理您创建的临时文件(您设置delete=False):

import os
f.close()
os.unlink(f.name)

或者,只需省略delete关键字,将其默认为True,然后再关闭您的文件,无需取消关联。

刚刚将存档文件名写入新文件..

您只是将新存档名称写入临时文件。你最好直接阅读档案:

data = open(make_archive(f.name, 'zip', root_dir), 'rb').read()

请注意,现在根本没有写入您的临时文件。

最佳方法

完全避免创建NamedTemporaryFile:改为使用tempfile.mkdtemp()生成一个临时目录,用于放置存档,然后清理它:

tmpdir = tempfile.mkdtemp()
try:
    tmparchive = os.path.join(tmpdir, 'archive')

    root_dir = "something"

    data = open(make_archive(tmparchive, 'zip', root_dir), 'rb').read()

finally:
    shutil.rmtree(tmpdir)
相关问题