curl断点续载

摘自http://blog.csdn.net/zmy12007/article/details/37157297

摘自http://www.linuxidc.com/Linux/2014-10/107509.htm

curl断点续传,下载过程中关闭控制台,然后重新启动,又会接着下载

  1. #include "stdafx.h"  
  2. #include <io.h>  
  3. #include "curl/curl.h"  
  4. #include <string>/*注意包含这个头文件后必须把share.h重命名一下,可能是stl里面也有这个头文件,比如curl_share.h,然后把包含到的地方替换一下*/  
  5.   
  6. #include "curl/easy.h"  
  7.   
  8. using namespace std;  
  9.   
  10. static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userdata)  
  11. {  
  12.     FILE *fp = (FILE*)userdata;  
  13.     size_t written = fwrite(ptr, size, nmemb, fp);  
  14.     return written;  
  15. }  
  16.   
  17. int assetsManagerProgressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded)  
  18. {  
  19.     static int percent = 0;  
  20.     int tmp = 0;  
  21.     long localLen = *(long*)ptr;  
  22.     if ( totalToDownload > 0 )  
  23.     {  
  24.         tmp = (int)((nowDownloaded + (double)localLen) / (totalToDownload + (double)localLen) * 100);  
  25.     }  
  26.       
  27.     printf("下载进度%0d%% ", tmp);  
  28.     return 0;  
  29. }  
  30.   
  31. long GetLocalFileLenth(const char* fileName)  
  32. {  
  33.     char strTemp[256] = {0};  
  34.     strcpy_s(strTemp,fileName);  
  35.     FILE* fp = fopen(strTemp, "rb");  
  36.     if(fp != NULL)  
  37.     {  
  38.         long localLen = _filelength(_fileno(fp));  
  39.         fclose(fp);  
  40.         return localLen;  
  41.     }  
  42.     return 0;  
  43. }  
  44.   
  45. /************************************************************************/  
  46. /* 获取要下载的远程文件的大小                                            */  
  47. /************************************************************************/  
  48. long getDownloadFileLenth(const char *url){  
  49.     long downloadFileLenth = 0;  
  50.     CURL *handle = curl_easy_init();  
  51.     curl_easy_setopt(handle, CURLOPT_URL, url);  
  52.     curl_easy_setopt(handle, CURLOPT_HEADER, 1);    //只需要header头  
  53.     curl_easy_setopt(handle, CURLOPT_NOBODY, 1);    //不需要body  
  54.     if (curl_easy_perform(handle) == CURLE_OK)   
  55.     {  
  56.         curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);  
  57.     }   
  58.     else   
  59.     {  
  60.         downloadFileLenth = -1;  
  61.     }  
  62.     return downloadFileLenth;  
  63. }  
  64.   
  65. bool downLoad(void *_curl, std::string _packageUrl, std::string _storagePath, std::string fileName )  
  66. {  
  67.     // Create a file to save package.  
  68.     const string outFileName = _storagePath + fileName;  
  69.     //================断点续载===================  
  70.     long localLen = GetLocalFileLenth(outFileName.c_str());  
  71.   
  72.     FILE *fp = fopen(outFileName.c_str(), "a+b");  
  73.     if (! fp)  
  74.     {  
  75.         return false;  
  76.     }  
  77.     fseek(fp, 0, SEEK_END);  
  78.   
  79.     // Download pacakge  
  80.     CURLcode res;  
  81.     curl_easy_setopt(_curl, CURLOPT_URL, _packageUrl.c_str());  
  82.     curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, downLoadPackage);  
  83.     curl_easy_setopt(_curl, CURLOPT_WRITEDATA, fp);  
  84.     curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false);  
  85.     curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, assetsManagerProgressFunc);  
  86.     curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L);  
  87.     curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_LIMIT, 1L);  
  88.     curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_TIME, 5L);  
  89.   
  90.     curl_easy_setopt(_curl, CURLOPT_HEADER, 0L);  
  91.     curl_easy_setopt(_curl, CURLOPT_NOBODY, 0L);  
  92.     curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);  
  93.     curl_easy_setopt(_curl, CURLOPT_RESUME_FROM, localLen);  
  94.   
  95.     curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, &localLen);  
  96.   
  97.   
  98.   
  99.     res = curl_easy_perform(_curl);  
  100.     curl_easy_cleanup(_curl);  
  101.     if (res != 0)  
  102.     {  
  103.         fclose(fp);  
  104.         return false;  
  105.     }  
  106.   
  107.     fclose(fp);  
  108.     return true;  
  109. }  
  110.   
  111. int _tmain(int argc, _TCHAR* argv[])  
  112. {  
  113.     CURL* _curl = curl_easy_init();  
  114.     if (! _curl)  
  115.     {  
  116.         return 0;  
  117.     }  
  118.   
  119.     downLoad(_curl, "http://ardownload.adobe.com/pub/adobe/reader/win/11.x/11.0.01/en_US/AdbeRdr11001_en_US.exe", "./", "AdbeRdr11001_en_US.exe");  
  120.     //downLoad(_curl, "http://localhost/MyWebServer.rar", "./", "MyWebServer.rar");  
  121.     getchar();  
  122.     return 0;  
原文地址:https://www.cnblogs.com/LiuYanYGZ/p/5602707.html