I wanted to create a Windows Service which copies content of the folder into a created archive. I was suggested to use libzip library for this purpouse. I've created this code, but now I do not know how to compile and link it properly. I am not using CMake for the project building in Visual Studio.
#include <iostream>
#include <filesystem>
#include <string>
#include <zip.h>
constexpr auto directory = "C:/.../Directory/";
constexpr auto archPath = "C:/.../arch.zip";
int Archive(const std::filesystem::path& Directory, const std::filesystem::path& Archive) {
int error = 0;
zip* arch = zip_open(Archive.string().c_str(), ZIP_CREATE, &error);
if (arch == nullptr)
throw std::runtime_error("Unable to open the archive.");
for (const auto& file : std::filesystem::directory_iterator(Directory)) {
const std::string filePath = file.path().string();
const std::string nameInArchive = file.path().filename().string();
auto* source = zip_source_file(arch, item.path().string().c_str(), 0, 0);
if (source == nullptr)
throw std::runtime_error("Error with creating source buffer.");
auto result = zip_file_add(arch, nameInArchive.c_str(), source, ZIP_FL_OVERWRITE);
if (result < 0)
throw std::runtime_error("Unable to add file '" + filePath + "' to the archive.");
}
zip_close(arch);
return 0;
}
int main() {
std::filesystem::path Directory(directory);
std::filesystem::path ArchiveLocation(archPath);
Archive(Directory, ArchiveLocation);
return 0;
}