2010-01-29 70 views
3

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

在此先感谢。

回答

0

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

无论如何,ftpgetresp.c示例显示了一种方法来做到这一点,我认为。 FTP Custom CUSTOMREQUEST暗示另一种方式。

+0

嗨,非常感谢。我能够使用FTP Custome CUSTOMREQUEST检索目录中的文件。我还有另外一个问题,推荐多个文件使用curl_multi或者一次传输一个文件? – Thi 2010-01-29 13:45:46

+0

我发现另一种方法来检索只使用CURLOPT_DIRLISTONLY选项的目录中的文件。我尝试使用CUSTOMREQUEST命令,我需要做大量的解析,但使用CURLOPT_DIRLISTONLY选项,我们可以只获取文件名而不是其他信息。 – Thi 2010-02-04 15:09:29

7

下面是一段代码示例。

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; 
} 
+5

是不是有写功能丢失? 'curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,GetFilesList_response);' – Tombart 2012-05-11 07:26:22