Libzip - Error: Error while opening the archive : no error

Libzip - Error: Error while opening the archive : no error

我正在努力寻找解决问题的方法; 事实上,我正在编写自己的工具,使用 C++ 中的 libzip 来压缩文件。

绝对没有完成,但我想做一些测试,然后我做了并从日志中得到一个 "funny" 错误。

这是我的函数:

void save(std::vector<std::string> filepath, std::string savepath){
int err;

savepath += time(NULL);
zip* saveArchive = zip_open(savepath.c_str(), ZIP_CREATE , &err);
if(err != ZIP_ER_OK) throw xif::sys_error("Error while opening the archive", zip_strerror(saveArchive));
for(int i = 0; i < filepath.size(); i++){
    if(filepath[i].find("/") == std::string::npos){}
    if(filepath[i].find(".cfg") == std::string::npos){
        err = (int) zip_file_add(saveArchive, filepath[i].c_str(), NULL, NULL);
        if(err == -1) throw xif::sys_error("Error while adding the files", zip_strerror(saveArchive));
    }

}

if(zip_close(saveArchive) == -1) throw xif::sys_error("Error while closing the archive", zip_strerror(saveArchive));
}

我得到一个=> Error : Error while opening the archive : No error 当然,我没有写任何 .zip。

如果你能帮助我,谢谢你!

zip_opendocumentation 表示它仅在打开失败时设置 *errorp。测试 saveArchive == nullptr 或将 err 初始化为 ZIP_ER_OK.

P.S。搜索 '/' 什么都不做。您是要在该块中放置 continue 吗?

另一条有问题的行是:

    savepath += time(NULL);

如果那是标准的 time 函数,那么 returns 是自纪元以来的秒数。这可能会被截断为一个字符,然后将该字符附加到文件名。这将导致文件名中出现奇怪的字符!我建议使用 std::chrono 转换为文本。