curl断点续载
摘自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;
- }
curl断点续载的更多相关文章
- scrapy爬虫之断点续爬和多个spider同时爬取
from scrapy.commands import ScrapyCommand from scrapy.utils.project import get_project_settings #断点续 ...
- 关于视频断点续播和H5的本地存储
前段时间,需要在下实现一个视频的断点续播功能,呃,我不会呀,这就很尴尬了.然后呢,在下就想起了一个叫做localStorage的东西.这是个什么东西呢?在网上查阅了一些资料后,在下发现这是webSto ...
- python3.6 单文件爬虫 断点续存 普通版 文件续存方式
# 导入必备的包 # 本文爬取的是顶点小说中的完美世界为列.文中的aa.text,bb.text为自己创建的text文件 import requests from bs4 import Beautif ...
- Electron 的断点续下载
最近用 Electron 做了个壁纸程序,需要断点续下载,在这里记录一下. HTTP断点下载相关的报文 Accept-Ranges 告诉客户端服务器是否支持断点续传,服务器返回 Content-Ran ...
- 解决win7 下 curl无法加载的问题
最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到了这个问题! 一.win7系统64位, apache2.2, php 5.35 vc6 版本 这个比较容易: 1. phpinfo ...
- Keras模型训练的断点续训、早停、效果可视化
训练:model.fit()函数 fit(x=None, y=None, batch_size=None, epochs=, verbose=, callbacks=None, validation_ ...
- HTML 5 断点续上传
断点上传,java里面比较靠谱一点的,一般都会选用Flex.我承认,Flex只是摸了一下,不精通.HTML 5 有个Blob对象(File对象继承它),这个对象有个方法slice方法,可以对一个文件进 ...
- scrapy 断点续爬
第一步:安装berkeleydb数据库 第二部:pip install bsddb3 第三部:pip install scrapy-deltafetch 第四部: settings.py设置 SPID ...
- python下载mp4 同步和异步下载支持断点续下
Range 用于请求头中,指定第一个字节的位置和最后一个字节的位置,一般格式: Range:(unit=first byte pos)-[last byte pos] Range 头部的格式有以下几种 ...
随机推荐
- Maximal Square 解答
Question Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1' ...
- 【C++基础之十四】函参的缺省
可能会有这么一个函数,在大部分的情况下,我们不用给它传递参数,但在某些特殊情况下,我们需要给它传递参数,那怎么办呢? 简单啊,写两个一样的方法,一个带参,一个不带参... 这样也太没水准了.来点高端的 ...
- go 学习笔记 - sublime text 环境配置
园里已经有了一篇相当不错的配置说明文章,只是现在gosublime不再支持2.x.文章里的操作在sublimetext3 里一样可以使用 文章地址 : http://www.cnblogs.com/s ...
- swift通过摄像头读取每一帧的图片,并且做识别做人脸识别
最近帮别人做一个项目,主要是使用摄像头做人脸识别 github地址:https://github.com/qugang/AVCaptureVideoTemplate 要使用IOS的摄像头,需要使用AV ...
- Angular之作用域与事件(转)
学习Angular,首先要理解其作用域机制. Angular应用是分层的,主要有三个层面:视图,模型,视图模型.其中,视图很好理解,就是直接可见的界面,模型就是数据,那么视图模型是什么呢?是一种把数据 ...
- 浅谈设计模式在GIS中的应用
设计模式在GIS中的应用 一.设计模式概述 随着面向对象技术的广泛应用,软件复用在越来越多的开发工程中被采用.在研究软件复用的过程中,设计模式的概念被提了出来.所谓设计模式就是一些设计面向对象的软件的 ...
- 关于ajax提交的公共接口的一大用处
在项目框架搭建的时候,就写了ajax提交的公共接口,是想统一的日志和处理ajax返回的错误信息. 今天,却又帮我解决了另外一个问题:每次点开某个页面,有一个ajax请求总是会调用两次,于是打开chro ...
- js文件代码未加载或者没有js效果
问题:在页面中js文件中的代码未加载或者没有任何效果. 原因: 成功引用了js文件,但无效果或者提示未加载该文档中的代码. 可能页面引用js文件的路径存在问题 解决: 重新检查你引用的js文件的路径是 ...
- [Non-original]OS X How do I unset an IP address set with ifconfig?
I recently used ifconfig en1 1.2.3.4 to set the IP address of a network interface (specifically, the ...
- Ecsotre 参考
2.dbschema dbschema 字段属性 ‘type’ => ‘int unsigned’, //字段类型 ‘extra’ => ‘auto_increment’,//定义自增 ‘ ...