我想创建一个 Windows 服务,它将文件夹的内容复制到创建的存档中。有人建议我为此目的使用libzip库。我已经创建了这段代码,但现在我不知道如何正确编译和链接它。我没有使用 CMake 在 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;
}