首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libzip: zip_name_locate()在特定文件名上失败,甚至尝试所有可能的编码组合

libzip: zip_name_locate()在特定文件名上失败,甚至尝试所有可能的编码组合
EN

Stack Overflow用户
提问于 2022-03-11 18:33:08
回答 1查看 63关注 0票数 0

我试图在libzip之上构建一个“故障安全”层,但是libzip在这里给我带来了一些麻烦。

首先,我用zip_file_add(...)将一个文件添加到我的(空)存档中。这有3种可能的用户定义编码可用。然后我尝试用zip_name_locate(...)定位这个名字,它还有3种可能的用户定义的编码。

此mcve检查所有可能的编码组合,所有这些组合对于特定的文件名x%²»Ã-ØÑ–6¨wx.txt都失败。当使用更传统的file.txt文件名时,zip_name_locate()每次都会成功。

代码语言:javascript
复制
#include <zip.h>
#include <include/libzip.h>//<.pragmas to include the .lib's...
#include <iostream>
#include <vector>
#include <utility>

/*
    'zip_file_add' possible encodings:
        ZIP_FL_ENC_GUESS
        ZIP_FL_ENC_UTF_8
        ZIP_FL_ENC_CP437

    'zip_name_locate' possible encodings:
        ZIP_FL_ENC_RAW
        ZIP_FL_ENC_GUESS
        ZIP_FL_ENC_STRICT
*/

/*
    build encoding pairs (trying all possibilities)
*/
std::vector<std::pair<unsigned, unsigned>>
encoding_pairs{
    { ZIP_FL_ENC_GUESS, ZIP_FL_ENC_RAW },
    { ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_RAW },
    { ZIP_FL_ENC_CP437, ZIP_FL_ENC_RAW },
    { ZIP_FL_ENC_GUESS, ZIP_FL_ENC_GUESS },
    { ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_GUESS },
    { ZIP_FL_ENC_CP437, ZIP_FL_ENC_GUESS },
    { ZIP_FL_ENC_GUESS, ZIP_FL_ENC_STRICT },
    { ZIP_FL_ENC_UTF_8, ZIP_FL_ENC_STRICT },
    { ZIP_FL_ENC_CP437, ZIP_FL_ENC_STRICT },
};

int main(int argc, char** argv) {

    const char* file_buf = "hello world";
#if 0
    const char* file_name = "file.txt";
#else
    const char* file_name = "x%²»Ã-ØÑ–6¨wx.txt";
#endif

    zip_error_t ze;
    zip_error_init(&ze);
    {
        zip_source_t* zs = zip_source_buffer_create(nullptr, 0, 1, &ze);
        if (zs == NULL)
            return -1;

        zip_t* z = zip_open_from_source(zs, ZIP_CHECKCONS, &ze);
        if (z == NULL)
            return -1;
        {
            zip_source_t* s = zip_source_buffer(z, file_buf, strlen(file_buf), 0);//0 = don't let libzip auto-free the const char* buffer on the stack
            if (s == NULL)
                return -1;

            for (size_t ep = 0; ep < encoding_pairs.size(); ep++) {
                std::cout << "ep = " << ep << std::endl;
                zip_uint64_t index;
                if ((index = zip_file_add(z, file_name, s, encoding_pairs[ep].first)) == -1) {
                    std::cout << "could not zip_file_add() with encoding " << encoding_pairs[ep].first << std::endl;
                    continue;
                }

                if (zip_name_locate(z, file_name, encoding_pairs[ep].second) == -1) {
                    std::cout << "the name '" << file_name << "' could not be located." << std::endl;
                    std::cout << " encoding pair: " << encoding_pairs[ep].first << " <-> " << encoding_pairs[ep].second << std::endl;
                }
                else {
                    std::cout << "the name was located." << std::endl;
                }

                if (zip_delete(z, index) == -1)
                    return -1;
            }
        }
        zip_close(z);
    }
    zip_error_fini(&ze);

    return 0;
}

我不明白我在这里可能做错了什么,或者如果libzip甚至不能解析这样的名称。

,如果它不能,那么在名称上要避免的标准是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-13 13:36:25

事实证明,问题在于我的源文件本身的编码。它是ANSI -所以我把它转换成UTF8,它解决了这个问题。

我仍然不明白的是,为什么libzip不能从输入c-字符串中zip_name_locate()一个与zip_file_add()中使用的输入c-字符串完全相同的名称(不管源文件编码是什么)。“迷失在翻译中”?

(特别感谢托马斯·克劳斯纳帮助我找到这个问题)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71443187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档