RAR files not downloading correctly using requests module

Question:

Recently, I was creating an installer for my project. It involves downloading a RAR file from my server, unRAR-ing it and putting those folders into the correct locations.

Once it’s downloaded, the program is supposed to unRAR it, but instead it gives me an error in the console: Corrupt header of file.
I should note that this error is coming from the unRAR program bundled with WinRAR.
I also tried opening the file using the GUI of WinRAR, and it gave the same error.
I’m assuming while it’s being downloaded, it’s being corrupted somehow?
Also, when I download it manually using a web browser, it downloads fine.
I’ve tried this code:

KALI = "URL CENSORED"
kali_res = requests.get(KALI, stream=True)
for chunk in kali_res.iter_content(chunk_size=128):
  open(file_path, "wb").write(chunk)

..but it still gives the same error.
Could someone please help?

Asked By: xavvvv

||

Answers:

You keep re-opening the file for every chunk.
Not only does this leak file descriptors, it also means you keep overwriting the file.

Try this instead:

KALI = "URL CENSORED"
kali_res = requests.get(KALI, stream=True)
with open(file_path, "wb") as outfile:
    for chunk in kali_res.iter_content(chunk_size=128):
        outfile.write(chunk)
Answered By: Roland Smith
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.