c++ - 压缩包 - 错误 : Error while opening the archive : no error

标签 c++ zip compression libzip

我正在尝试找出解决问题的方法; 事实上,我正在编写自己的工具,使用 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));
}

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

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

最佳答案

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

附言搜索 '/' 什么都不做。你是想在那个 block 中放一个 continue 吗?

另一条有问题的行是:

    savepath += time(NULL);

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

关于c++ - 压缩包 - 错误 : Error while opening the archive : no error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37364760/

相关文章:

c++ - 如何在 Ubuntu 16.10 上使用 Conan 和 SDL2 解决 undefined reference 错误?

C++:像 PHP 一样将大量数字存储在 float 中?

java - 将本地目录中的 zip 文件放入 java spring boot 中的列表中

archive - .NET 中字节数组解压缩中的 LZH?

c++ - 带有 const 模板和 const 引用模板参数的 std::map

C++ 无效参数,候选为 : ".." on Setters from different classes

c++ - 使用 zlib 附加到压缩文件

css - 如何从 Compass 输出压缩的 CSS?

php - 在 Mac OS X 上为 PHP --enable-zip 安装 zip 扩展

python - 函数列表和参数的映射 : unpacking difficulty