【问题标题】:Downloading all files in directory using libcurl使用 libcurl 下载目录中的所有文件
【发布时间】:2010-01-29 12:59:17
【问题描述】:

我是 libcurl 的新手,并找到了一种从 ftp 服务器下载单个文件的方法。现在我的要求是下载一个目录中的所有文件,我猜它不受 libcurl 支持。请在 libcurl 上建议如何下载目录中的所有文件或者是否有任何其他类似于 libcurl 的库?

提前致谢。

【问题讨论】:

    标签: c++ download libcurl ftps


    【解决方案1】:

    这是一段示例代码。

    static size_t GetFilesList_response(void *ptr, size_t size, size_t nmemb, void *data)
    {
        FILE *writehere = (FILE *)data;
        return fwrite(ptr, size, nmemb, writehere);
    }
    
    bool FTPWithcURL::GetFilesList(char* tempFile)
    {
        CURL *curl;
        CURLcode res;
        FILE *ftpfile;
    
        /* local file name to store the file as */
        ftpfile = fopen(tempFile, "wb"); /* b is binary, needed on win32 */ 
    
        curl = curl_easy_init();
        if(curl) 
        {
            curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com");
            curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
            // added to @Tombart suggestion
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetFilesList_response);
            curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
    
            res = curl_easy_perform(curl);
    
            curl_easy_cleanup(curl);
        }
    
        fclose(ftpfile); //
    
    
        if(CURLE_OK != res) 
            return false;
    
        return true;
    }
    

    【讨论】:

    • 没有写功能吗? curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetFilesList_response);
    【解决方案2】:

    您需要 FTP 服务器上的文件列表。这并不简单,因为每个 FTP 服务器可能会返回不同格式的文件列表...

    无论如何,我认为ftpgetresp.c 示例显示了一种方法。 FTP Custom CUSTOMREQUEST 建议另一种方式。

    【讨论】:

    • 嗨,非常感谢。我能够使用 FTP Custome CUSTOMREQUEST 检索目录中的文件。我还有一个问题,对于多个文件,建议使用 curl_multi 还是一次传输一个文件?
    • 我找到了另一种使用 CURLOPT_DIRLISTONLY 选项仅检索目录中文件的方法。我尝试使用 CUSTOMREQUEST 命令,我需要进行大量解析,但使用 CURLOPT_DIRLISTONLY 选项,我们只能获取文件名而不是其他信息。
    【解决方案3】:

    只需使用 CURLOPT_WILDCARDMATCH 功能。 示例代码: https://curl.haxx.se/libcurl/c/ftp-wildcard.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2015-05-18
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多