开发者

Corrupted Binary Files after Transfer libcurl

开发者 https://www.devze.com 2023-01-10 12:50 出处:网络
I am transferring a binar开发者_如何学Pythony file (.exe) with FTP using libcurl, and saving it to a local file. The problem is that after the file is transferred, it is altered and is no longer a val

I am transferring a binar开发者_如何学Pythony file (.exe) with FTP using libcurl, and saving it to a local file. The problem is that after the file is transferred, it is altered and is no longer a valid Win32 application, and doesn't run. Here's how I'm doing it:

CURL *curl;

curl = curl_easy_init();
    FILE* f = fopen("C:\\blah.exe", "w");

if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.mysite.com");
    curl_easy_setopt(curl, CURLOPT_USERPWD, "blah:blah");
    curl_easy_setopt(curl, CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_SINGLECWD);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
} else {
            fclose(f);
    return CURL_EASY_INIT_FAIL;
}

    fclose(f);

The file is written but is bigger than it is on the FTP server. Like I said, trying to run it results in the "%1 is not a valid Win32 application" error. Did I forget to set an option or something?


You forgot the binary flag. This is the correct code:

 FILE* f = fopen("C:\\blah.exe", "wb");


The reason is that you transfer as ASCII and not as binary. So your end of lines might get broken. Of there are CRs in the binary they might turn into CR LF or the other way around. Tune CURL to make a binary transfer.

0

精彩评论

暂无评论...
验证码 换一张
取 消