'django export xls as zip doesn't work: format unknown

I want to make a view that produce xls file using xlwt and make file downloaded as zip file using zipfile.

def export(request):
    
    filename = "myfile.xls"

    wb = xlwt.Workbook(encoding='utf-8')
    ....
    buffer1 = io.BytesIO()
    wb.save(buffer1)
    
    buffer2 = io.BytesIO()
    with zipfile.ZipFile(buffer2, mode='w') as zip_file:
        zip_file.writestr(zinfo_or_arcname=filename, data=buffer1.getvalue())

    response = HttpResponse(content_type='application/x-zip-compressed')
    response['Content-Disposition'] = 'attachment; filename=myzip.zip'
    return response

but when I try to unzip I got an error: file format unknown



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source