libcurl控制台进度条,用于文件下载 [英] libcurl console progress bar for file download

查看:435
本文介绍了libcurl控制台进度条,用于文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我实现了用于从服务器下载文件的代码.它的工作正常. 现在,我要创建自己的进度条功能,该功能可以计算一些数据,例如剩余秒数据每秒的速率等. 因此,从此处找到了一种使用curl进度条选项的方法.我们如何启用此选项. 我完全做到了.

Here I implemented code for file download from server. its working fine. Now I want to make my own progress bar function which calculates some data like remaining seconds data Rate per second etc. So from here I found one way to use curl progress bar option. how we can enable this option. I completely done with this.

我将代码放在下面.在此代码中,my_progress_func在每个curl库时间间隔内频繁调用.我想更改此间隔时间并将其设置为1秒.可以在curl库中使用为curl库设置一些选项吗?

I put my code below. here in this code my_progress_func calls frequently as per curl library time interval. I want to change this interval time and make it to 1 second. is it possible in curl library using to set some options for curl library?

我想每1秒钟调用一次此my_progress_func函数.

I want to call this my_progress_func function after every 1 second.

代码:

#include <stdio.h>
#include <curl/curl.h>

long test =0;

struct FtpFile {
  const char *filename;
  FILE *stream;
  long iAppend;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
      out->stream=fopen(out->filename, out->iAppend ? "ab":"wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  out->iAppend += nmemb;
  return fwrite(buffer, size, nmemb, out->stream);
}

int my_progress_func(void *bar,
                     double t, /* dltotal */ 
                     double d, /* dlnow */ 
                     double ultotal,
                     double ulnow)
{
    printf("%f : %f \n", d, t);

  return 0;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  int c;

  struct FtpFile ftpfile={
    "dev.zip", /* name to store the file as if succesful */
    NULL,
  };

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
                     "sftp://root:xyz_@192.170.10.1/mnt/xyz.tar");


    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 120L);


    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);


    curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");

    /* Switch on full protocol/debug output */

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);

    res = curl_easy_perform(curl);

    printf("res is %d\n, data get %ld\n", res, ftpfile.iAppend);


    ///Retry upto 100 times it timeout or connection drop occur
    for (c = 0; (res != CURLE_OK) && (c < 100); c++) {



        curl_easy_setopt(curl, CURLOPT_RESUME_FROM , ftpfile.iAppend);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK) c =0;
        printf("%d res is %d\n, data get %ld\n",c, res, ftpfile.iAppend);

    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */
  curl_global_cleanup();
   return 0;
}

推荐答案

根据curl文档: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

According to the curl documentation: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

应该与curl_progress_callback相匹配的函数指针 原型发现于. libcurl调用此函数 而不是内部等价 操作(大约每秒一次或更早) 是否转移.未知/未使用的参数值传递给 回调将设置为零(例如,如果您仅下载数据,则 上传大小将保持为0).从此返回非零值 回调将导致libcurl中止传输并返回 CURLE_ABORTED_BY_CALLBACK.

Function pointer that should match the curl_progress_callback prototype found in . This function gets called by libcurl instead of its internal equivalent with a frequent interval during operation (roughly once per second or sooner) no matter if data is being transfered or not. Unknown/unused argument values passed to the callback will be set to zero (like if you only download data, the upload size will remain 0). Returning a non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.

如果调用频率太高,则可以使用time()和一个静态var来限制它,如下所示:

If it's calling too frequently then you can use time() and a static var to limit this, something like this:

static time_t prevtime;
time_t currtime;
double dif;
static int first = 1;
if(first) {
    time(&prevtime);
    first = 0;
}
time(&currtime);
dif = difftime(currtime, prevtime);
if(dif < 1.0)
    return;
prevtime = currtime;

显然,您冒着curl可能在另一秒钟后再次调用此函数的风险.

Obviously, you run the risk that curl might not call this function again for fully another second.

这篇关于libcurl控制台进度条,用于文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆