POCO 的 Zip 类对中文文件名支持不正确的解决方法

来源:互联网 发布:scipy python 编辑:程序博客网 时间:2024/04/27 14:23


POCO 在 Windows 中默认是定义了 POCO_WIN32_UTF8

#if defined (POCO_WIN32_UTF8)std::wstring utf16Path;UnicodeConverter::toUTF16(path, utf16Path);_handle = CreateFileW(utf16Path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);#else_handle = CreateFileA(path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);#endif

所以从这段代码来看,对路径的操作是默认使用 UTF16 的。但是在 Zip 里面,文件名不是用 UTF16 来存放。

Poco::Path file(filename); // filename 不是 utf16 编码file.makeFile();Poco::Path dest(_outDir, file); // _outDir 是 utf16 编码dest.makeFile();...Poco::FileOutputStream out(dest.toString()); // 这时候整个文件路径是不正确的ZipInputStream inp(zipStream, hdr, false);Poco::StreamCopier::copyStream(inp, out);out.close();

只要把 filename 转换成 UTF16 就可以了。

在 Poco::Path file(filename); 之前加上这段代码,整个文件路径就正确了:

std::string dest_filename = fileName;#if defined (POCO_WIN32_UTF8)std::wstring utf16_name = s2ws(fileName);UnicodeConverter::toUTF8(utf16_name, dest_filename);#endifPoco::Path file(dest_filename);

std::wstring Decompress::s2ws(const std::string& s){setlocale(LC_ALL, "chs"); const char* _Source = s.c_str();size_t _Dsize = s.size() + 1;wchar_t *_Dest = new wchar_t[_Dsize];wmemset(_Dest, 0, _Dsize);mbstowcs(_Dest,_Source,_Dsize);std::wstring result = _Dest;delete []_Dest;setlocale(LC_ALL, "C");return result;}



原创粉丝点击