cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Beginner trying to download a file

From: Jeff Pohlmeyer <yetanothergeek_at_gmail.com>
Date: Sun, 14 Jun 2009 11:12:03 -0500

On Sun, Jun 14, 2009 at 1:34 AM, J. Anderson<lordmaiku_at_gmail.com> wrote:
> I'm trying to download a smallish (1.5 MB) zip file over HTTP and having a
> little trouble. The file transfer appears to work correctly, but the
> downloaded file is corrupted
...

> The file this code downloads is very slightly larger than the original file

You don't say what version of libcurl you're using, or what OS you're
running on,
but if you happen to be using MS-Windows, you'll need to fopen() the file using
"wb" instead of just "w"

The code you posted seems to work fine here, after a few tweaks:

* You really don't need a ReadFunc at all for what you're doing,
that's generally only
  used for uploading or POST data, etc.

* I don't see the point in testing if the file exists before you
download it. If it doesn't exist,
  you'll find out soon enough anyway. But if you really want to do it
that way, you should
  use CURLOPT_NOBODY for the first request, that way you will just download the
  response headers (HEAD) without downloading the whole file twice.

* You should always test the return value of curl_easy_perform()
before you check the
  CURLINFO_RESPONSE_CODE, it's only valid if you actually receive a
response from
  the server.

- Jeff
Received on 2009-06-14