歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

curl 實現下載文件特定片段

通過設置CURLOPT_RANGE可以下載服務器端文件的特定數據塊,這樣如果服務器數據很大的情況下,如果只是需其中一小塊數據就可以采用這種方法

當然這個也可以用來多線程加速下載同一個文件

#include "stdafx.h"
#include "curl/curl.h"
#include <string>/*注意包含這個頭文件後必須把share.h重命名一下,可能是stl裡面也有這個頭文件,比如curl_share.h,然後把包含到的地方替換一下*/

#include "curl/easy.h"

using namespace std;

struct RecordNode
{
public:
 int idx;
 int data[10000];
 void SetData(int i)
 {
  idx = i;
  for ( int j = 0; j < 10000; j ++ )
  {
   data[j] = i + j;
  }
 }
};

struct BufferNode
{
 char ptrBuff[40004];
 int cur;
 int max;
};

BufferNode node;

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;
 BufferNode* pBuf = /*&node;*/(BufferNode*)userdata;
 size_t cnt = size * nmemb;
 if ( pBuf->cur + nmemb <= pBuf->max )
 {
  memcpy(&(pBuf->ptrBuff[pBuf->cur]), ptr, cnt);
  pBuf->cur += cnt;
 }
 else
 {
  //printf("over flow...\n");
  memcpy(&(pBuf->ptrBuff[pBuf->cur]), ptr, pBuf->max - pBuf->cur);
  pBuf->cur += pBuf->max - pBuf->cur;

 }

 return nmemb;
}

int assetsManagerProgressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded)
{
 static int percent = 0;
 int tmp = 0;
 if ( totalToDownload > 0 )
 {
  tmp = (int)(nowDownloaded / totalToDownload * 100);
 }
 
 printf("下載進度%0d%%\r", tmp);
 return 0;
}

bool downLoad(void *_curl, std::string _packageUrl, int idx )
{
 // Create a file to save package.
 //BufferNode node;
 node.cur = 0;
 node.max = sizeof(RecordNode);
 //node.ptrBuff = new char[node.max];
 char szRange[128] ={};
 size_t nodeSize = sizeof(RecordNode);
 sprintf( szRange, "%d-%d", idx * nodeSize, (idx + 1)*nodeSize );

 // 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, &node);
 curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false);
 curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, assetsManagerProgressFunc);
 //curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, this);
 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_TIMEOUT, 20);        //設置超時
 curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1);        //屏蔽其它信號
 curl_easy_setopt(_curl, CURLOPT_HEADER, 0);          //下載數據包括HTTP頭部
 curl_easy_setopt(_curl, CURLOPT_RANGE, /*"0-500"*/szRange);    //用於斷點續傳, 設置下載的分片

 res = curl_easy_perform(_curl);
 curl_easy_cleanup(_curl);
 if (res != 0)
 {
  printf("error: failed to download data\n");
  return false;
 }

 if(node.cur == node.max)
 {
  RecordNode record;
  memcpy(&record, node.ptrBuff, sizeof(RecordNode));
  printf(".....idx:%d,data3:%d.......\n", record.idx, record.data[2]);
 }
 else
 {
  printf("error: data not correct\n");
 }

 //delete[] node.ptrBuff;

 return true;
}

void GenerateData()
{
 FILE *fp = fopen("records.dat", "wb");
 if (!fp)
 {
  return;
 }

 for ( int i = 0; i < 10000; i ++ )
 {
  RecordNode node;
  node.SetData(i);
  size_t written = fwrite(&node, sizeof(RecordNode), 1, fp);
 }

 fclose(fp);

 fp = fopen("records.dat", "rb");
 if (!fp)
 {
  return;
 }
 RecordNode nodeBuf;
 fread(&nodeBuf, sizeof(RecordNode), 1, fp);
 printf("idx:%d,data3:%d\n", nodeBuf.idx, nodeBuf.data[2]);
 fseek(fp,-1 * int(sizeof(RecordNode)), SEEK_END);
 fread(&nodeBuf,sizeof(RecordNode), 1, fp);
 printf("idx:%d,data3:%d\n", nodeBuf.idx, nodeBuf.data[2]);
 fclose(fp);
}

int _tmain(int argc, _TCHAR* argv[])
{
 //GenerateData();

 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/records.dat", 1);
 _curl = curl_easy_init();
 downLoad(_curl, "http://localhost/records.dat", 10);
 _curl = curl_easy_init();
 downLoad(_curl, "http://localhost/records.dat", 28);
 getchar();
 return 0;
}

Ubuntu 用戶安裝下載器 cURL 7.36.0  http://www.linuxidc.com/Linux/2014-05/102269.htm

Linux curl使用簡單介紹 http://www.linuxidc.com/Linux/2008-01/10891.htm

Unix下Curl的使用方法及常用功能記錄分享 http://www.linuxidc.com/Linux/2012-08/69154.htm

curl命令使用 http://www.linuxidc.com/Linux/2014-09/107018.htm

Copyright © Linux教程網 All Rights Reserved