Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:49:45+00:00 2026-06-13T08:49:45+00:00

I am trying to return a zip file in django http response, the code

  • 0

I am trying to return a zip file in django http response, the code goes something like…

archive = shutil.make_archive('testfolder', 'zip', MEDIA_ROOT, 'testfolder')
response = HttpResponse(FileWrapper(open(archive)),
                           content_type=mimetypes.guess_type(archive)[0])
response['Content-Length'] = getsize(archive)
response['Content-Disposition'] = "attachment; filename=test %s.zip" % datetime.now()
return response

Now when this code is executed on ubuntu the resulting downloaded file opens without any issue, but when its executed on windows the file created does not open in winzip (gives error ‘Unsupported Zip Format’).

Is there something very obvious I am missing here? Isn’t python code supposed to be portable?

EDIT:

Thanks to J.F. Sebastian for his comment…

There was no problem in creating the archive, it was reading it back into the request. So, the solution is to change second line of my code from,

response = HttpResponse(FileWrapper(open(archive)),
                           content_type=mimetypes.guess_type(archive)[0])

to,

response = HttpResponse(FileWrapper(open(archive, 'rb')),  # notice extra 'rb'
                           content_type=mimetypes.guess_type(archive)[0])

checkout, my answer to this question for more details…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:49:46+00:00Added an answer on June 13, 2026 at 8:49 am

    Thanks to J.F. Sebastian for his comment…

    I’ll still write the solution here in detail…

    There was no problem in creating the archive, it was reading it back into the request. So, the solution is to change second line of my code from,

    response = HttpResponse(FileWrapper(open(archive)),
                               content_type=mimetypes.guess_type(archive)[0])
    

    to,

    response = HttpResponse(FileWrapper(open(archive, 'rb')),  # notice extra 'rb'
                               content_type=mimetypes.guess_type(archive)[0])
    

    because apparently, hidden somewhere in python 2.3 documentation on open:

    The most commonly-used values of mode are ‘r’ for reading, ‘w’ for
    writing (truncating the file if it already exists), and ‘a’ for
    appending (which on some Unix systems means that all writes append to
    the end of the file regardless of the current seek position). If mode
    is omitted, it defaults to ‘r’. The default is to use text mode, which
    may convert ‘\n’ characters to a platform-specific representation on
    writing and back on reading. Thus, when opening a binary file, you
    should append ‘b’ to the mode value to open the file in binary mode,
    which will improve portability.
    (Appending ‘b’ is useful even on
    systems that don’t treat binary and text files differently, where it
    serves as documentation.) See below for more possible values of mode.

    So, in simple terms while reading binary files, using open(file, 'rb') increases portability of your code (it certainly did in this case)

    Now, it extracts without troubles, on windows…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to unzip some zip file, it has about 65 megs. Code
i am trying to have a REST service return a zip file from the
I'm trying to return a zip file, as a stream over a browser. This
I'm trying ZIP a bundle of File's and return ZIP through a servlet. This
I'm trying to make a method of compressing file using Ionic.Zip.dll of DotNetZip. Code
I'm trying to develop an AQGridView in iPhone. I downloaded the .zip file of
Hy guys, I'm trying to generate a Zip File with ICSharpCode.SharpZipLib library but it's
I am trying to deploy a zip file to a remote inhouse maven repo.(artifactory
For some reason when trying to upload a zip file this function always returns
I am trying to convert an array of bytes into a ZIP file. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.