N
N
NyxDeveloper2021-01-20 10:01:14
Django
NyxDeveloper, 2021-01-20 10:01:14

How can I create a zipfile.Zipfile in the folder I need?

I do in my application the return of all files from a folder in one archive. Everything is cool, the archive is being created, but for some reason, when downloading, it has no name, and inside there is a full path to files from empty directories. How do I create an archive file in the media/archs/ folder and how do I assign a name to the archive when downloading?

views.py

def DownloadAllFilesView(request, objId):
    if request.method == 'POST':
        project_folder = Doc.objects.filter(idProj=Project.objects.get(id=objId)).first().idProj.name.replace(' ', '_')
        path = settings.MEDIA_ROOT + '/project_%s/' % project_folder
        z_name = '%s.zip' % Project.objects.get(id=objId).name.replace(' ', '_')

        z = zipfile.ZipFile(z_name, 'w')  # Создание нового архива

        for file in os.listdir(path):
                z.write(os.path.join(path, file))  # Создание относительных путей и запись файлов в архив

        z.close()
        return FileResponse(open(z.filename, 'rb'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-20
@NyxDeveloper

inside there is a full path to files from empty directories.
https://docs.python.org/3/library/zipfile.html#zip...
How do I create an archive file in the media/archs/ folder
in z_name specify the absolute path to this location.
how to assign a name to an archive when downloading
you the third time to give a link to everything on the same?
PS well, let's already learn to read the docks on your own

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question