How to receive a zip file from server: C/C++

2019-08-30 02:18发布

I am using libcurl and able to receive json response and saving it to file.

Below is the code

    #include <curl/curl.h>
    #include <curl/types.h>
    #include <curl/easy.h>
    #include <string.h>

    #define URL "http://www.joes-hardware.com/tools.html"
    size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
        size_t written;
        written = fwrite(ptr, size, nmemb, stream);
        return written;
    }

    int main(void) {
        CURL *curl;
        FILE *fp;
        CURLcode res;
        //char *url = "http://www.joes-hardware.com/tools.html";

        char *url= URL;
        char outfilename[FILENAME_MAX] = "./json";
        curl = curl_easy_init();
        if (curl) {
            fp = fopen(outfilename,"wb");
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
            fclose(fp);
        }
        return 0;
    }

As part of the application which I am developing, now it has to fetch a zip file from the server.

Suppose the URL is of the format shown below:

#define URL "https://Server/File.zip"

For such URL, the code is not able to save the zip file. How to achieve this?

I am developing for LINUX platform.

标签: c++ c curl zip
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-30 02:26

Resolved the Issue. Problem was with HTTPS connection Added certificate Based on below links:

Can't connect to HTTPS site using cURL. Returns 0 length content instead. What can I do?

Getting no content from a HTTPS connection using CURL

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
#include <stdlib.h>


#define false 0

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}


int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    //char *url = "http://www.joes-hardware.com/tools.html";

    char *url= "https://HOST/FileName.zip";
    //char *url=    "https://wordpress.org/";
    char outfilename[FILENAME_MAX] = "./json.zip";


    curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);

    if(vinfo->features & CURL_VERSION_SSL){
        printf("CURL: SSL enabled\n");
    }else{
        printf("CURL: SSL not enabled\n");
    }


    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        /* Setup the https:// verification options - note we do this on all requests as there may
           be a redirect from http to https and we still want to verify */
        //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
        curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);


        int i=fclose(fp);
        if( i==0)
        system("unzip -j json.zip");
    }
    return 0;
}
查看更多
登录 后发表回答