title: Getting corrupt zips using Python ZipStream in Django
body: <p>I am a little new to Django so please bear with me.<br>
  I'm using zipstream from <a href="https://github.com/allanlei/python-zipstream" rel="nofollow">here</a> and have a Django view that returns a zip file of all file attachments which are all hosted on Amazon S3. But the zip files are all coming up as corrupt when I download them, that is, I can't open them. I have tried verifying the files with <code>unzip -t</code> but the errors are not very helpful.</p>
  
  <pre><code>file_paths = [fa.file.url for fa in file_attachments.all()]
  
  zf = zipstream.ZipFile(mode='w', compression=zipstream.ZIP_DEFLATED)
  
  zip_subdir = "Attachments-%s" % (request_id)
  
  for file_path in file_paths:
      file_dir, file_name = os.path.split(file_path)
  
      zf.writestr(file_name, urllib.urlopen(file_path).read())
  
  zip_filename = "%s.zip" % (zip_subdir)
  
  response = StreamingHttpResponse(zf, mimetype='application/zip')
  response['Content-Disposition'] = \
      'attachment; filename={}'.format(zip_filename)
  return response
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Solved it.</p>
  
  <pre><code>s = StringIO.StringIO()
  with zipstream.ZipFile(s, mode='w', compression=zipstream.ZIP_DEFLATED) as zf:
      #could fail on url open.
      for file_path in file_paths:
          file_dir, file_name = os.path.split(file_path)
  
          try:
              file_contents = urllib.urlopen(file_path).read()
              zf.writestr(file_name, file_contents)
          except IOError: #connection cannot be made
              logging.error()
  
  response = StreamingHttpResponse(s.getvalue(), mimetype='application/octet-stream')
  response['Content-Disposition'] = \
      'attachment; filename={}'.format("%s" % (request_id))
  return response
  </code></pre>