Google

星期三, 一月 23, 2008

Auto update with wxHTTP and ofstream

#include
#include

//[...]
// progress bar with 100 units ... declared as wxGauge progress;
progress = new wxGauge(this, ID_Progress, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL);
//[...]

bool GetUpdate::DownloadUpdate()
{
wxHTTP http;
http.SetTimeout(120);
http.Connect(_T("www.example.com"));
wxInputStream *httpStream = http.GetInputStream(_T("/file.exe"));

if (http.GetError() == wxPROTO_NOERR)
{
size_t chunks = 100;
size_t chunkSize = httpStream->GetSize() / chunks;
char *fileContent = new char[chunkSize];

wxFFile file(_T("file.exe"), _T("wb"));

for (size_t i = 0; i <= chunks; i++)
{
progress->SetValue(i * 100/chunks);
httpStream->Read(fileContent, chunkSize);

file.Write(fileContent, httpStream->LastRead());
}
file.Flush();

wxDELETE(fileContent);
}

http.Close();
wxDELETE(httpStream);
return true;
}

标签: ,

0 条评论:

发表评论

<< 主页

辽ICP备05003652号
流风洄雪听天籁,轻云蔽日看落花

Powered by Blogger