Minizip-使用fopen与UTF -8文件名

问题描述 投票:0回答:2

在Windows范围内使用_wfopen

用于Unicode项目。请注意,它接受Unicode(UTF-16)字符串,而不是UTF-8。对于UTF-8,标准

fopen

具有额外的选项
c++ utf-8 unzip multiplatform
2个回答
2
投票

FILE *fp = fopen("newfile.txt", "rt+, ccs=encoding");


    

在当今情况下,情况要好一些,您不应修改
minizip
的来源,但是在您的代码中仍然需要一个
#ifdef


2
投票
上的文件路径中支持

niCode。最新版本具有用于读/写zip的新API函数:

unzopen2_64(const voidPath,zlib_filefunc64_defpzlib_filefunc_def)

  • zipopen2_64(const void pathname,int append,zipcharpcglobalcomment,zlib_filefunc64_def* pzlib_filefunc_def) 参数
  • zlib_filefunc64_def
  • 可以指结构,其中包括用于使用文件系统(或内存)的功能集。此外,minizip软件包提供标题文件'IOWIN32.h',它具有使用
  • WindowsAPI
的填充结构的方法。因此,对于打开文件,您可以使用以下代码:

zlib_filefunc64_def
我发现的第二个工作选项 - 只需将其设置为UTF -8即可。最好在应用程序初始化层进行操作,因为它不是线程安全的:
zipFile openZipFile(const std::string& utf8FilePath)
{
    zipFile hZipFile = nullptr;
    #ifdef WIN32
        zlib_filefunc64_def ffunc;
        fill_win32_filefunc64W(&ffunc);

        // Convert path from UTF-8 to Unicode
        const int count = MultiByteToWideChar(CP_UTF8, 0, utf8FilePath.c_str(), static_cast<int>(utf8FilePath.size()), NULL, 0);
        std::wstring unicodePath(count, 0);
        MultiByteToWideChar(CP_UTF8, 0, utf8FilePath.c_str(), static_cast<int>(utf8FilePath.size()), &unicodePath[0], count);

        hZipFile = zipOpen2_64(unicodePath.c_str(), 0, NULL, &ffunc);
    #else
        // Unix systems handles path in the UTF-8 by default
        hZipFile = zipOpen64(utf8FilePath.c_str(), 0);
    #endif

    return hZipFile;
}
它看起来要简单得多,但是我认为第一个变体是更好和可靠的,因为它不取决于语言环境的设置。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.