在django中输出.zip文件

时间:2015-07-10 14:39:31

标签: python django django-views

我想上传带有.csv文件的zip文件,并输出带.vm文件的zip文件。 我使用这段代码:

def csv_archive_to_vm(request):
response = HttpResponse(content_type='application/force-download')

work_string = ''

if request.method == "POST":
    ##reading input zip file
    input_file = request.FILES.get('file')
    zf = zipfile.ZipFile(input_file)
    for info in zf.infolist():
        ##reading files in archive
        path = re.search('(.*\.csv)', info.filename)
        path_name = re.search('(.*/)(.*\.csv)', info.filename)
        for string in zf.open(info.filename):

            quotes_search = re.search('"(.*)",\s*"(.*)",\s*"(.*)"', string)
            if quotes_search:
                descr = quotes_search.group(1)
                macro_name = quotes_search.group(2)
                say = quotes_search.group(3)

            new_lines_search = re.search('/n', say)
            if new_lines_search:
                say = re.sub('/n', '\n\t\t', say)
            ##making content for new files for new archive
            work_string = work_string + '##' + descr + '\n#macro(' + macro_name + ')\n\t#random()\n\t\t' + say + '\n\t#end\n#end\n\n'
        ##outputting new archive
        zipdata = StringIO()
        zf_create = zipfile.ZipFile(zipdata, mode='a')
        try:
            if path_name:
                zf_create.writestr(str(path_name.group(1)) + str(path_name.group(2))[0:-4] + '.vm', work_string)
        finally:
            zf_create.close()
        work_string = ''
        response = HttpResponse(zipdata.read())
        response['Content-Disposition'] = 'attachment; filename=assistant-linguistics_vm.zip'
        response['Content-Type'] = 'application/x-zip'

return response

但我得到空的zip存档,重量为0kb。我究竟做错了什么?感谢。

0 个答案:

没有答案