操作curl下载文件(进度条显示) 代码片断
副标题#e#
在项目中需要用到措施更新的成果,同事先容说是curl中的开拓库很牛x,又是跨平台(他们 老是这么喜欢跨平台的对象 *_*),于是下载这个包测试了一下,确实不错。筹备正式用到项 目中,以下一个例子用于从互联网上抓取一个文件下载到当地,并加长进度条显示,做得挺 简略,不外成果差不多就这样了。
措施运行预览.
首先需要 插手多线程的机制,因为措施一边在下载文件,一边在显示进度条,单线程的方法必定不可 ,所以我用到了wxTimer来实现,在downloadMain.h 中界说了一个wxTimer,并做了事件申 明.
DECLARE_EVENT_TABLE()
/***************************************************************
* Name: downloadMain.h
* Purpose: Defines Application Frame
* Author: (alan)
* Created: 2008-11-14
* Copyright: (谦泰 通讯)
* License:
**************************************************************/
#ifndef DOWNLOADMAIN_H
#define DOWNLOADMAIN_H
#include "downloadApp.h"
#include <wx/timer.h>
#include "GUIDialog.h"
class downloadDialog: public GUIDialog
{
public:
downloadDialog(wxDialog *dlg);
~downloadDialog();
void OnTimer(wxTimerEvent& event);
private:
virtual void OnClose (wxCloseEvent& event);
virtual void OnQuit (wxCommandEvent& event);
virtual void OnAbout (wxCommandEvent& event);
void downloadfile();
wxTimer* m_timerdown;
DECLARE_EVENT_TABLE()
};
#endif // DOWNLOADMAIN_H
#p#副标题#e#
下面是主措施的代 码.
/***************************************************************< br />* Name: downloadMain.cpp
* Purpose: Code for Application Frame
* Author: (alan)
* Created: 2008-11-14
* Copyright: (谦泰通讯)
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "downloadMain.h"
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "update.h"
#include <wx/msgdlg.h>
#include <wx/utils.h>
#define TIMER_ID 22222
//事件监听声明
BEGIN_EVENT_TABLE(downloadDialog, GUIDialog)
EVT_TIMER(TIMER_ID, downloadDialog::OnTimer)
END_EVENT_TABLE()
enum wxbuildinfoformat
{
short_f, long_f
};
wxString wxbuildinfo (wxbuildinfoformat format)
{
wxString wxbuild (wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("- Windows");
#elif defined(__WXMAC__)
wxbuild << _T("-Mac");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
//声明一个 文件布局体
struct FtpFile
{
char *filename;
FILE *stream;
};
downloadDialog::downloadDialog(wxDialog *dlg)
: GUIDialog(dlg)
{
//建设一个按时器,拟定 TIMER_ID
m_timerdown = new wxTimer(this, TIMER_ID);
// 按时器开始运行,这里会自动执行OnTimer函数
m_timerdown->Start (100);
}
downloadDialog::~downloadDialog()
{
}
//按时器 操纵
void downloadDialog::OnTimer(wxTimerEvent &event)
{
downloadfile();
}
//文件写入流
int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if (out && !out->stream)
{
out->stream=fopen(out->filename, "wb");
if (!out->stream)
{
return -1;
}
}
return fwrite(buffer, size, nmemb, out->stream);
}
// 进度条显示函数
int wxcurldav_dl_progress_func(void* ptr, double rDlTotal, double rDlNow, double rUlTotal, double rUlNow)
{
wxGauge* pGauge = (wxGauge*) ptr;
if(pGauge)
//配置进度条的值
pGauge->SetValue(100.0 * (rDlNow/rDlTotal));
return 0;
}
//下载文件函数
void downloadDialog::downloadfile()
{
//建设curl工具
CURL *curl;
CURLcode res;
m_staticText2->SetLabel(wxT("请耐性期待措施下载更新包..."));
struct FtpFile ftpfile=
{
//界说下载到当地的文件位置和路径
"tmp.exe",NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
//curl初始 化
curl = curl_easy_init();
//curl工具存在的环境下执行 操纵
if (curl)
{
//配置远端地点
curl_easy_setopt(curl, CURLOPT_URL,"http://dl_dir.qq.com/minigamefile/QQGame2008ReleaseP2_web_setup .EXE");
//执行写入文件流操纵
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
//curl的进度条声明
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
//回调进度条函数
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, wxcurldav_dl_progress_func);
//设 置进度条名称
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, m_gauge1);
//进度条
m_gauge1- >SetValue(0);
//写入文件
res = curl_easy_perform(curl);
m_gauge1->SetValue(100);
//释放curl工具
curl_easy_cleanup(curl);
if (CURLE_OK != res)
;
}
if (ftpfile.stream)
{
//封锁文件 流
fclose(ftpfile.stream);
}
//释放全局curl工具
curl_global_cleanup();
//这一步很重要,遏制按时器,否则措施会无休止的运行下去
m_timerdown->Stop();
//执行刚下载完毕的措施,举办措施更 新
int pid = ::wxExecute(_T("tmp.exe"));
wxMessageBox(wxT("下载完毕,措施开始执行更新操 作......"));
}
void downloadDialog::OnClose(wxCloseEvent &event)
{
Destroy();
}
void downloadDialog::OnQuit(wxCommandEvent &event)
{
Destroy ();
}
void downloadDialog::OnAbout(wxCommandEvent &event)
{
}
本文出自 “阿汐的博客” 博客,请务必保存此出处 http://axiii.blog.51cto.com/396236/112836