cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to download a zip file using http

From: Mark Hessling <mark_at_rexx.org>
Date: Thu, 21 Nov 2013 06:59:04 +1000

On 21/11/13 03:04, stuart_at_semag.juplo.com wrote:
>
> On 2013-11-20 00:40, Mark Hessling wrote:
>
>
> It is working; all Zip files start with PK, so you are downloading
> the zip file but displaying it to stdout.
>>
>> So how do i download a zip from a url?
>>
> To capture the output in a file have a look at the
> CURLOPT_WRITEFUNCTION of curl_easy_setopt().
>
> Cheers, Mark
>
> okay i got a few questions here.
>
> First Where does it save this file too.
>
> Second I changed my code now and i dont get the PK thing anymore.
> it might not be downloading it or something is wrong.
>
> here is the code i have now.
>
> #include <stdio.h>
> #include <curl.h>
> #include <types.h>
> #include <easy.h>
> #include <string>
> #include <iostream>
>
> using namespace std;
>
> size_t write_data(void *ptr, size_t size, size_t nmemb, FILE
> *stream) {
> size_t written = fwrite(ptr, size, nmemb, stream);
> return written;
> }
>
> int main(void) {
> CURL *curl;
> FILE *fp;
> CURLcode res;
> char *url = "http://test.com/update/testt.zip";
> char outfilename[FILENAME_MAX] = "test.zip";
> curl = curl_easy_init();
> if (curl) {
> fp = fopen(outfilename,"wb");
> if (fp !=NULL)
> {
> cout << "file opened\n";
> }
> curl_easy_setopt(curl, CURLOPT_URL,
> "http://test.com/update/testt.zip");
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
> res = curl_easy_perform(curl);
> /* always cleanup */
> curl_easy_cleanup(curl);
> fclose(fp);
> system("PAUSE");//so i can see output
> }
> return 0;
> }
>
As you didn't specify a directory on the filename you opened, the file
test.zip is created in the same directory from where you ran the program.

I changed the URL to my website, compiled your program; (had to change
<types.h> to <sys/types.h>; using MinGW), and ran it. It worked fine.

Cheers, Mark

-- 
------------------------------------------------------------------------
* Mark Hessling, mark@rexx.org http://www.rexx.org/
* Author of THE, a Free XEDIT/KEDIT editor and, Rexx/SQL, Rexx/CURL, etc.
* Maintainer of Regina Rexx interpreter
* Use Rexx? join the Rexx Language Association: http://www.rexxla.org/
------------------------------------------------------------------------

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-11-20