3

I am new to the libcurl and found a way to download a single file from the ftp server. Now my requirement is to download all files in a directory and i guess it was not supported by libcurl. Kindly suggest on libcurl how to download all files in directory or is there any other library similar to libcurl?

Thanks in advance.

4

3 回答 3

10

这是一段示例代码。

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;
}
于 2011-03-12T12:21:58.623 回答
0

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

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

于 2010-01-29T13:14:19.560 回答
0

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

于 2020-10-23T03:05:36.493 回答