2

My question is similar in nature to this very helpful answer, but I want to stream in and out simultaneously.

I have a django app that stores references to files on an external http fileserver. Right now when a user requests a zip collection it does the following (pseudo):

generate session_id
for url in url_list:
    download file to sessionid/filename.ext
for file in session_id folder:
    zip.write
close
http response

Obviously this is less than ideal as it: 1. Requires cleanup, 2. Is slow and 3. causes a long delay before the user sees any download progress.

The bit I'm unable to re-code is the io buffer/"file like object". Zipfile looks for a file on write but I want to provide a stream. In brief, how can I pipe requests to zipfile to HttpResponse?

Community
  • 1
  • 1
Linef4ult
  • 143
  • 1
  • 9

1 Answers1

0

You can use the writestr command with a streaming file download, I recommend the requests library.

Zipfile.writestr Documentation

ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])

Edit: Sample

zipinfo = ...
zip = ...

for url in list:
    r = requests.get(url, stream=True)
    for line in r.iter_lines():
        # filter out keep-alive new lines
        if line:
            print(zip.writestr(zipinfo, line))
pypypy
  • 1,075
  • 8
  • 18