【问题标题】:Libzip - Error: Error while opening the archive : no errorLibzip - 错误:打开存档时出错:没有错误
【发布时间】:2016-05-21 15:23:47
【问题描述】:

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

绝对没有完成,但我想做一些测试,然后我做了并从日志中获得了一个“有趣”的错误。

这是我的功能:

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));
}

我收到了=&gt; Error : Error while opening the archive : No error 当然,我没有写任何 .zip。

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

【问题讨论】:

    标签: c++ zip compression libzip


    【解决方案1】:

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

    附:搜索'/' 什么都不做。您的意思是在该块中添加continue 吗?

    另一个有问题的行是:

        savepath += time(NULL);
    

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

    【讨论】:

    • 我没有再得到这个愚蠢的“没有错误”,但它也没有创建一个 .zip 文件……搜索“/”还没有实现。感谢您的回复。
    • 在调试器中单步执行。向量中有什么东西吗?添加返回什么?
    • 我想我不明白 zip_file_add 是如何工作的。实际上,函数不会压缩我的文件并将其添加到存档中,但它只是在其中创建了一个路径。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2011-07-13
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多