3

私はlibcurlを初めて使用し、ftpサーバーから単一のファイルをダウンロードする方法を見つけました。今私の要件はディレクトリ内のすべてのファイルをダウンロードすることです、そして私はそれがlibcurlによってサポートされていなかったと思います。libcurlでディレクトリ内のすべてのファイルをダウンロードする方法を提案してください。または、libcurlに類似した他のライブラリはありますか?

前もって感謝します。

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カスタム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 に答える