-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a simple downloader with curl and qt
- Loading branch information
1 parent
631706a
commit 318134f
Showing
8 changed files
with
263 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,11 @@ public slots: | |
* | ||
*/ | ||
void onListItemSelected(); | ||
/** | ||
* | ||
*/ | ||
void installTrle(); | ||
|
||
|
||
private: | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "fileop.h" | ||
#include "quazip/quazip.h" | ||
#include "quazip/quazipfile.h" | ||
|
||
FileManager::FileManager(QObject *parent){} | ||
|
||
const QString FileManager::calculateMD5(const QString& filename) | ||
{ | ||
QFile file(filename); | ||
if (!file.open(QIODevice::ReadOnly)) | ||
{ | ||
qDebug() << "Error opening file for reading: " << file.errorString(); | ||
return ""; | ||
} | ||
|
||
QCryptographicHash md5(QCryptographicHash::Md5); | ||
|
||
char buffer[1024]; | ||
qint64 bytesRead; | ||
|
||
while ((bytesRead = file.read(buffer, sizeof(buffer))) > 0) | ||
{ | ||
md5.addData(buffer, static_cast<int>(bytesRead)); | ||
} | ||
|
||
file.close(); | ||
return QString(md5.result().toHex()); | ||
} | ||
/** | ||
* | ||
*/ | ||
void FileManager::extractZip(const QString& zipFilename, const QString& extractPath) | ||
{ | ||
QuaZip zip(zipFilename); | ||
if (!zip.open(QuaZip::mdUnzip)) | ||
{ | ||
qDebug() << "Error opening ZIP file" << Qt::endl; | ||
return; | ||
} | ||
|
||
QuaZipFileInfo info; | ||
QuaZipFile file(&zip); | ||
|
||
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) | ||
{ | ||
if (zip.getCurrentFileInfo(&info) && file.open(QIODevice::ReadOnly)) | ||
{ | ||
QByteArray data = file.readAll(); | ||
file.close(); | ||
|
||
// Create directories if they don't exist | ||
QString filePath = extractPath + QDir::separator() + info.name; | ||
QString directory = filePath.left(filePath.lastIndexOf(QDir::separator())); | ||
|
||
QDir().mkpath(directory); | ||
|
||
// Create a new file on disk and write the data | ||
QFile outFile(filePath); | ||
if (outFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) | ||
{ | ||
outFile.write(data); | ||
outFile.close(); | ||
} | ||
else if (!filePath.endsWith('/')) | ||
{ | ||
qDebug() << "Error opening file for writing: " << filePath << Qt::endl; | ||
} | ||
} | ||
else | ||
{ | ||
qDebug() << "Error reading file info from ZIP archive" << Qt::endl; | ||
} | ||
} | ||
zip.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef FILEOP_H | ||
#define FILEOP_H | ||
|
||
#include <QObject> | ||
#include <QFile> | ||
#include <QDir> | ||
#include <QCryptographicHash> | ||
#include <QDebug> | ||
|
||
class FileManager : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit FileManager(QObject *parent = 0); | ||
const QString calculateMD5(const QString& filename); | ||
void extractZip(const QString& zipFilename, const QString& extractPath); | ||
|
||
private: | ||
|
||
}; | ||
#endif // FILEOP_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "network.h" | ||
|
||
Downloader::Downloader(QObject *parent) : | ||
QObject(parent){} | ||
|
||
size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) { | ||
size_t total_size = size * nmemb; | ||
QByteArray* buffer = static_cast<QByteArray*>(userp); | ||
buffer->append(static_cast<char*>(contents), total_size); | ||
return total_size; | ||
} | ||
void Downloader::setUrl(QUrl url){ url_m=url; } | ||
void Downloader::setSavePath(QString path){ path_m=path; } | ||
|
||
void Downloader::saveToFile(const QByteArray& data, const QString& filePath) { | ||
QFile file(filePath); | ||
if (file.open(QIODevice::WriteOnly)) { | ||
file.write(data); | ||
file.close(); | ||
qDebug() << "Data saved to file:" << filePath; | ||
} else { | ||
qDebug() << "Error saving data to file:" << file.errorString(); | ||
} | ||
} | ||
void Downloader::run() | ||
{ | ||
if (url_m.isEmpty()) {return;} | ||
QString urlString = url_m.toString(); | ||
QByteArray byteArray = urlString.toUtf8(); | ||
const char* url_cstring = byteArray.constData(); | ||
|
||
if (path_m.isEmpty()) {return;} | ||
|
||
// Initialize libcurl | ||
curl_global_init(CURL_GLOBAL_DEFAULT); | ||
|
||
// Initialize libcurl easy handle | ||
CURL* curl = curl_easy_init(); | ||
if (curl) { | ||
// Create a QByteArray to store the downloaded data | ||
QByteArray data; | ||
|
||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); | ||
curl_easy_setopt(curl, CURLOPT_URL, url_cstring); | ||
|
||
// Perform the download | ||
CURLcode res = curl_easy_perform(curl); | ||
|
||
if (res != CURLE_OK) { | ||
qDebug() << "curl_easy_perform() failed:" << curl_easy_strerror(res); | ||
} else { | ||
qDebug() << "Downloaded successful\n"; | ||
saveToFile(data, path_m); | ||
} | ||
curl_easy_cleanup(curl); | ||
} | ||
curl_global_cleanup(); | ||
} |
Oops, something went wrong.