curl断点续载
2016-06-21 10:12
357 查看
摘自http://blog.csdn.net/zmy12007/article/details/37157297
摘自http://www.linuxidc.com/Linux/2014-10/107509.htm
curl断点续传,下载过程中关闭控制台,然后重新启动,又会接着下载
#include "stdafx.h"
#include <io.h>
#include "curl/curl.h"
#include <string>/*注意包含这个头文件后必须把share.h重命名一下,可能是stl里面也有这个头文件,比如curl_share.h,然后把包含到的地方替换一下*/
#include "curl/easy.h"
using namespace std;
static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userdata)
{
FILE *fp = (FILE*)userdata;
size_t written = fwrite(ptr, size, nmemb, fp);
return written;
}
int assetsManagerProgressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded)
{
static int percent = 0;
int tmp = 0;
long localLen = *(long*)ptr;
if ( totalToDownload > 0 )
{
tmp = (int)((nowDownloaded + (double)localLen) / (totalToDownload + (double)localLen) * 100);
}
printf("下载进度%0d%%\r", tmp);
return 0;
}
long GetLocalFileLenth(const char* fileName)
{
char strTemp[256] = {0};
strcpy_s(strTemp,fileName);
FILE* fp = fopen(strTemp, "rb");
if(fp != NULL)
{
long localLen = _filelength(_fileno(fp));
fclose(fp);
return localLen;
}
return 0;
}
/************************************************************************/
/* 获取要下载的远程文件的大小 */
/************************************************************************/
long getDownloadFileLenth(const char *url){
long downloadFileLenth = 0;
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_HEADER, 1); //只需要header头
curl_easy_setopt(handle, CURLOPT_NOBODY, 1); //不需要body
if (curl_easy_perform(handle) == CURLE_OK)
{
curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);
}
else
{
downloadFileLenth = -1;
}
return downloadFileLenth;
}
bool downLoad(void *_curl, std::string _packageUrl, std::string _storagePath, std::string fileName )
{
// Create a file to save package.
const string outFileName = _storagePath + fileName;
//================断点续载===================
long localLen = GetLocalFileLenth(outFileName.c_str());
FILE *fp = fopen(outFileName.c_str(), "a+b");
if (! fp)
{
return false;
}
fseek(fp, 0, SEEK_END);
// Download pacakge
CURLcode res;
curl_easy_setopt(_curl, CURLOPT_URL, _packageUrl.c_str());
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, downLoadPackage);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false);
curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, assetsManagerProgressFunc);
curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_TIME, 5L);
curl_easy_setopt(_curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(_curl, CURLOPT_NOBODY, 0L);
curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(_curl, CURLOPT_RESUME_FROM, localLen);
curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, &localLen);
res = curl_easy_perform(_curl);
curl_easy_cleanup(_curl);
if (res != 0)
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
CURL* _curl = curl_easy_init();
if (! _curl)
{
return 0;
}
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");
//downLoad(_curl, "http://localhost/MyWebServer.rar", "./", "MyWebServer.rar");
getchar();
return 0;
}
摘自http://www.linuxidc.com/Linux/2014-10/107509.htm
curl断点续传,下载过程中关闭控制台,然后重新启动,又会接着下载
#include "stdafx.h"
#include <io.h>
#include "curl/curl.h"
#include <string>/*注意包含这个头文件后必须把share.h重命名一下,可能是stl里面也有这个头文件,比如curl_share.h,然后把包含到的地方替换一下*/
#include "curl/easy.h"
using namespace std;
static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userdata)
{
FILE *fp = (FILE*)userdata;
size_t written = fwrite(ptr, size, nmemb, fp);
return written;
}
int assetsManagerProgressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded)
{
static int percent = 0;
int tmp = 0;
long localLen = *(long*)ptr;
if ( totalToDownload > 0 )
{
tmp = (int)((nowDownloaded + (double)localLen) / (totalToDownload + (double)localLen) * 100);
}
printf("下载进度%0d%%\r", tmp);
return 0;
}
long GetLocalFileLenth(const char* fileName)
{
char strTemp[256] = {0};
strcpy_s(strTemp,fileName);
FILE* fp = fopen(strTemp, "rb");
if(fp != NULL)
{
long localLen = _filelength(_fileno(fp));
fclose(fp);
return localLen;
}
return 0;
}
/************************************************************************/
/* 获取要下载的远程文件的大小 */
/************************************************************************/
long getDownloadFileLenth(const char *url){
long downloadFileLenth = 0;
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_HEADER, 1); //只需要header头
curl_easy_setopt(handle, CURLOPT_NOBODY, 1); //不需要body
if (curl_easy_perform(handle) == CURLE_OK)
{
curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);
}
else
{
downloadFileLenth = -1;
}
return downloadFileLenth;
}
bool downLoad(void *_curl, std::string _packageUrl, std::string _storagePath, std::string fileName )
{
// Create a file to save package.
const string outFileName = _storagePath + fileName;
//================断点续载===================
long localLen = GetLocalFileLenth(outFileName.c_str());
FILE *fp = fopen(outFileName.c_str(), "a+b");
if (! fp)
{
return false;
}
fseek(fp, 0, SEEK_END);
// Download pacakge
CURLcode res;
curl_easy_setopt(_curl, CURLOPT_URL, _packageUrl.c_str());
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, downLoadPackage);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false);
curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, assetsManagerProgressFunc);
curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_TIME, 5L);
curl_easy_setopt(_curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(_curl, CURLOPT_NOBODY, 0L);
curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(_curl, CURLOPT_RESUME_FROM, localLen);
curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, &localLen);
res = curl_easy_perform(_curl);
curl_easy_cleanup(_curl);
if (res != 0)
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
CURL* _curl = curl_easy_init();
if (! _curl)
{
return 0;
}
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");
//downLoad(_curl, "http://localhost/MyWebServer.rar", "./", "MyWebServer.rar");
getchar();
return 0;
}
相关文章推荐
- Android 最简单的SD卡文件遍历程序
- jq监听ajax执行开始,出错,结束。
- spring3.2 @Scheduled注解 定时任务
- jedis与spring整合及简单的使用RedisTemplate操作
- Cookie和Session的区别
- RedHat 7配置NFS服务
- 数学测试4
- 三分钟了解Activity工作流
- android 数据存储的五大方式(转)
- launchpad MSP430G2553 软件UART和硬件UART及跳帽的设置
- 未整理笔记
- JS技巧
- “Invalid maximum heap size” when running Maven
- android-R.drawable
- containsKey() 用法
- 网站登录 - 密码如何加密
- JHTP练习题及课题_第四章_控制语句Part 1-赋值、++、--运算符
- GCC 显示程序详细编译-链接过程
- CentOS下pdf阅读器
- js同步和异步编程