Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Last active September 28, 2022 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/57a999fe2c3587db078f75d64d23d353 to your computer and use it in GitHub Desktop.
Save ryanbekabe/57a999fe2c3587db078f75d64d23d353 to your computer and use it in GitHub Desktop.
C/CPP/C++ cURL Download File
// 14:20 28/09/2022
// Adapted from https://curl.haxx.se/libcurl/c/https.html
// Source https://www.devdungeon.com/content/curl-tutorial, https://stackoverflow.com/questions/4984391/cmd-line-rename-file-with-date-and-time
// disable : 4996 https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(C4996)%26rd%3Dtrue&view=msvc-170
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#pragma warning(disable : 4996)
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int dirExists(const char* path)
{
struct stat info;
if (stat(path, &info) != 0)
return 0;
else if (info.st_mode & S_IFDIR)
return 1;
else
return 0;
}
int main(void)
{
CURL* curl;
CURLcode result;
FILE* fp;
//char* url = "http://www.hanyajasa.com/";
curl_global_init(CURL_GLOBAL_DEFAULT);
const char* path = "report";
const char* pathreportc = "C:\\simrs\\report";
std::ifstream inFile;
inFile.open("server.bkb"); //open the input file: http://192.168.88.100/report1.zip
std::stringstream strStream;
strStream << inFile.rdbuf(); //read the file
std::string strserver = strStream.str(); //str holds the content of the file
std::cout << strserver << "\n"; //you can do anything with the string!!!
std::cout << " " << std::endl;
printf("%d\n", dirExists(path));
if ("%d\n", dirExists(path) == 1) {
std::cout << "Folder report sudah ada, tak perlu download ulang ke server." << std::endl;
if ("%d\n", dirExists(pathreportc) == 1) {
std::cout << "Rename C_simrs_report." << std::endl;
system("move C:\\simrs\\report C:\\simrs\\report_%time:~0,2%%time:~3,2%-%DATE:/=%");
}
system("IF exist C:\\simrs\\report\\ (echo Direktori report sudah ada di C, rename dulu.) ELSE IF exist report (move report C:\\simrs\\report && echo Menyesuaikan report sesuai keperluan.)");
std::cout << "Selesai...." << std::endl;
system("pause");
exit(0);
}
system("IF exist C:\\simrs\\ (echo Direktori SIMRS sudah ada di C.) ELSE (mkdir C:\\simrs\\ && echo Direktori SIMRS belum ada maka akan dibuatkan di C.)");
//system("IF exist C:\\simrs\\report\\ (echo Direktori report sudah ada di C, rename dulu.) ELSE IF exist report (move report C:\\simrs\\report && echo Menyesuaikan report sesuai keperluan.)");
std::cout << "Bismillah, mulai download report...." << std::endl;
//system("echo Bismillah, mulai download report....");
curl = curl_easy_init();
//char outfilename[FILENAME_MAX] = "C:\\simrs\\report.zip";
char outfilename[FILENAME_MAX] = "report.zip";
if (curl) {
fp = fopen(outfilename, "wb");
// Set URL
//curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.88.100/report1.zip");
//curl_easy_setopt(curl, CURLOPT_URL, str + "/report1.zip");
curl_easy_setopt(curl, CURLOPT_URL, strserver);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
// If you want to set any more options, do it here, before making the request.
// Perform the request which prints to stdout
result = curl_easy_perform(curl);
// Error check
if (result != CURLE_OK) {
std::cerr << "Error during curl request: "
<< curl_easy_strerror(result) << std::endl;
}
curl_easy_cleanup(curl);
fclose(fp);
std::cout << "Selesai mendownload report.zip, silakan extract dulu, lalu jalankan kembali program ini." << std::endl;
}
else {
std::cerr << "Error initializing curl." << std::endl;
}
curl_global_cleanup();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment