如何使用 libcurl 从 github 版本下载 .zip/rar 存档?

问题描述 投票:0回答:1

我有一个问题,如何从 github 版本下载存档 (.zip/.rar)。对于简单的 .html 页面,它可以正常工作,但对于 github 版本则不行。奇怪的是,libcurl 从

CURL_OK
返回
curl_easy_perform
状态,但甚至不尝试写入任何数据块。

这是我的代码:


typedef size_t(*CurlWriteDataFn)(void* ptr, size_t size, size_t nmemb, FILE* stream);

// My write function (simply used from examples)
static size_t CurtWriteData(void* ptr, size_t size, size_t nmemb, FILE* stream) {
    size_t written = fwrite(ptr, size, nmemb, stream);
    return written;
}

// Donwload function
static void DownloadFile(const std::string& url, const std::string outFilePath, CurlWriteDataFn writeFn) {

    // Check url and outFilePath.
    if (url.empty()) {
        // Curl: URL is not set.
        return;
    }
    if (outFilePath.empty()) {
        // Curl: Output file path is not set.
        return;
    }

    CURL* curl;
    FILE* fp;
    CURLcode res;

    curl = curl_easy_init();
    if (!curl) {
        // Curl: Failed to initialize library.
        return;
    }

    // Copying url and outFilepath. (so char* can be always avaliable)
    String _url         = url;
    String _outFilePath = outFilePath;

    fp = fopen(_outFilePath.c_str(), "wb");
    if (!fp) {
        // Curl: Failed to fopen file
        curl_easy_cleanup(curl);
        return;
    }

    // Set options for library.
    curl_easy_setopt(curl, CURLOPT_URL, _url.c_str());
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Dark Secret Ninja/1.0");
    
    // NOTE: Also i try different UserAgents like this: 
    // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"); 
    // but still not work
    
    if (writeFn)
       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFn);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);

    // Process donwloading.
    res = curl_easy_perform(curl);
    if (res != CURLcode::CURLE_OK) {
        // Curl: Failed to download file.
    }

    curl_easy_cleanup(curl);
    fclose(fp);
}

int main() {
   // -- OK
   // DownloadFile("https://curl.se/mail/lib-2010-11/0182.html", "C:\\MyDownloads\\some.json", CurtWriteData); 
   
   // -- Failed (without any error code)
   DownloadFile("https://github.com/KennyProgrammer/TestRepo/archive/refs/tags/v0.0.1.zip", "C:\\MyDownloads\\SomeRelease-v0.0.1.zip", CurtWriteData);
   
   return 0;
}

注意:我还尝试将

url
http而不是https一起使用,并尝试验证对等主机名, 但还是不行。

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // verify ssl peer
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L); // verify ssl hostname

对于如何解决这个问题有什么建议吗?我将非常感激。

c++ libcurl
1个回答
0
投票

您可以使用

-v
标志在详细模式下从命令行调用curl来查看发生了什么

curl -v https://github.com/KennyProgrammer/TestRepo/archive/refs/tags/v0.0.1.zip -o 1.zip

它显示服务器响应重定向响应:

< HTTP/1.1 302 Found
...
< Location: https://codeload.github.com/KennyProgrammer/TestRepo/zip/refs/tags/v0.0.1

您需要处理它来查询实际位置 url,例如使用 CURLOPT_FOLLOWLOCATION 选项:

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
© www.soinside.com 2019 - 2024. All rights reserved.