Как загрузить файл .two txt в формате zip?

Я хочу загрузить test.txt и text1.txt как динамически созданный zip файл. test.txt и test1.txt файл загружается из базового каталога моего проекта. когда я вызываю функцию, ошибки не происходит и статус 200. может ли кто-нибудь поправить меня для извлечения zip файла test.txt и test1.txt.

enter image description here

# # FIXME: Change this (get paths from DB etc)    
        filenames = []
        #load the test.txt file from base directory
        filename1 = os.path.join(settings.BASE_DIR, 'test.txt')
        filename2 = os.path.join(settings.BASE_DIR, 'test1.txt')
        filenames.append(filename1)
        filenames.append(filename2)
        
        # # Folder name in ZIP archive which contains the above files    
        # # E.g [thearchive.zip]/somefiles/file2.txt    
        # # FIXME: Set this to something better    
        zip_subdir = "somefiles"    
        zip_filename = "%s.zip" % zip_subdir    
        # # Open StringIO to grab in-memory ZIP contents   
        # s = StringIO()   
        s = BytesIO() 
        # # The zip compressor    
        zf = zipfile.ZipFile(s, "w")   
        for fpath in filenames:        
        # # Calculate path for file in zip        
            fdir, fname = os.path.split(fpath)        
        
            zip_path = os.path.join(zip_subdir, fname)        
            # Add file, at correct path   
        
            zf.write(fpath, zip_path)
        print(zf)
        # # Must close zip for all contents to be written    
        zf.close()    
        # # Grab ZIP file from in-memory, make response with correct MIME-type    
        resp = HttpResponse(s.getvalue(), content_type = "application/zip")    
        # # ..and correct content-disposition    
        resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename   
        #return response for download
        return resp
Вернуться на верх