/*! * Email: scictor@gmail.com * Auth: scictor * Date: 6/9/2020 * File: DownloaderMainT.cpp * Class: %{Cpp:License:ClassName} (if applicable) * variable: * Brief: * Note: */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "Downloader.h" #pragma comment(lib,"libcurl.lib") int _tmain(int argc, char* argv[]) { DWORD tick = GetTickCount(); CDownloader murl; DLIO mDlWork; strcpy(mDlWork.url, "http://sw.bos.baidu.com/sw-search-sp/software/f69ab46476e8e/TGPSetup_2.3.2.4083.exe"); strcpy(mDlWork.filePath, ".\DownloadSoft\"); murl.AddDownloadWork(mDlWork); //添加到下载任务中 strcpy(mDlWork.url, "http://sw.bos.baidu.com/sw-search-sp/software/16f6d358815f2/iTunes_12.5.1.21.exe"); strcpy(mDlWork.filePath, ".\DownloadSoft\"); murl.AddDownloadWork(mDlWork); //添加到下载任务中 murl.StartDownloadThread(); //开启下载线程 CURDI curInfo; double curDownloadLen,preLen = 0.0; while(1) { if(murl.IsDownloadBegin()) { murl.GetCurrentDownloadInfo(&curInfo); //获取每次下载的信息(一次相当于毫秒级,这里速度也用毫秒计算) curDownloadLen = curInfo.CurDownloadLen; printf("正在下载:%s,下载进度:%6.2lf%%,下载速度:%9.2lfKB/s ",curInfo.fileName, ((double)curInfo.preLocalLen+curInfo.CurDownloadLen)/curInfo.totalFileLen*100,(curDownloadLen-preLen)/(double)(GetTickCount()-tick)); tick = GetTickCount(); Sleep(500); } if(murl.IsDownloadEnd()) break; preLen = curDownloadLen; } return 0; }
/*! * Email: scictor@gmail.com * Auth: scictor * Date: 6/9/2020 * File: Downloader.cpp * Class: Downloader (if applicable) * variable: * Brief: * Note: */ #include "Downloader.h" //#include <io.h> CDownloader::CDownloader(void) { m_downloadCourse = -1; m_nConnectTimeOut = 0; curl_global_init (CURL_GLOBAL_ALL); for(int i=0; i<MAXWORK; i++) { memset(m_dowloadWork->url, 0, 512); memset(m_dowloadWork->filePath, 0, 256); } m_curIndex = 0; } CDownloader::~CDownloader(void) { curl_global_cleanup(); } bool CDownloader::IsDownloadBegin() { if(m_downloadCourse == 0) return true; return false; } bool CDownloader::IsDownloadEnd() { if(m_downloadCourse == 1) return true; return false; } bool CDownloader::CreateMultiDir(const char* pathName) { if(pathName == NULL) return false; char filePath[256] = {0}; strcpy(filePath, pathName); int i = 0, pathLen = strlen(pathName); CString curPath; char curFilePath[256] = {0}; WIN32_FIND_DATA swf; if(filePath[pathLen - 1] != '\') //最后一个非0字符不是‘\’则加上 { filePath[pathLen] = '\'; } while(filePath[i] != '