1

I am creating a windows service that periodically copies the contents of a folder to an archive. I've searched for archiving libraries and found suggestions to use XZip. Now I can create a new archive using code like this:

HZIP arch = CreateZip((void*)Archive.c_str(), 0, ZIP_FILENAME);
if (arch == nullptr)
    return addLogMessage("Unable to open archive.");

for (const auto& file : std::filesystem::directory_iterator(Directory)) {
    const std::wstring filePath = file.path().wstring();
    const std::wstring nameInArchive = file.path().filename().wstring();

    if (ZipAdd(arch, (TCHAR*)nameInArchive.c_str(), (TCHAR*)filePath.c_str(), 0, ZIP_FILENAME) != ZR_OK) {
        std::string what = "Error: Adding '" + std::string(filePath.begin(), filePath.end()) + "' to the archive.";
        addLogMessage(what.c_str());
    }
}

CloseZip(arch);

I also want to add files to an existing archive. When I run the code below, I get a ZR_ZMODE error from the ZipAdd function, which means tried to mix create / open zip archive:

if (std::filesystem::exists(Archive)) {
    HZIP arch = OpenZip((void*)Archive.c_str(), 0, ZIP_FILENAME);
    if (arch == nullptr)
        return addLogMessage("Unable to re-open existing archive.");

    for (const auto& file : std::filesystem::directory_iterator(Directory)) {
        const std::wstring filePath = file.path().wstring();
        const std::wstring nameInArchive = file.path().filename().wstring();

        if (ZipAdd(arch, (TCHAR*)nameInArchive.c_str(), (TCHAR*)filePath.c_str(), 0, ZIP_FILENAME) != ZR_OK) {
            std::string what = "Error: Adding '" + std::string(filePath.begin(), filePath.end()) + "' to the archive.";
            addLogMessage(what.c_str());
        }
    }

    CloseZip(arch);
}

I assume that in XZip library, I'm not able to add files to the existing archive. Is it true?

EDIT: I've solved the problem with different library, libzip. Now I can add new files to the old archive, and everything works fine. I've posted source code and compiling/linking instruction here.

I am still interested, if there is an option for adding files to the existing archive in XZip library, so I will not close the question for now.

Alvov1
  • 157
  • 1
  • 2
  • 10
  • Be careful of the casting of string types. if you have to cast to silence an error your are likely doing something wrong. – drescherjm Jan 06 '22 at 13:24
  • I believe libzip will allow you to do this but it will be more work to use in msvc. [https://libzip.org/](https://libzip.org/) You may want to use `vcpkg` to build the libraries for you: [https://vcpkg.io/en/packages.html](https://vcpkg.io/en/packages.html) and [https://github.com/microsoft/vcpkg](https://github.com/microsoft/vcpkg) – drescherjm Jan 06 '22 at 13:59
  • Yes yes yes. Thank you for the advise. I've switched to the libzip already, and it works pretty fine. Remains to connect it with the MSVC. And thanks for the advise about stirng's casting. – Alvov1 Jan 06 '22 at 14:32
  • Why there is need for library? Why not to forward the task to operating system with powershell compress-archive or the like? – Öö Tiib Jan 06 '22 at 14:43
  • This is the task. I cannot use system calls. – Alvov1 Jan 06 '22 at 14:47

0 Answers0