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

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

分类: C/C++ 和 Python   512人阅读  评论(0)  收藏  举报
path null file access string windows


POCO 在 Windows 中默认是定义了 POCO_WIN32_UTF8

[cpp]  view plain copy
  1. #if defined (POCO_WIN32_UTF8)  
  2.     std::wstring utf16Path;  
  3.     UnicodeConverter::toUTF16(path, utf16Path);  
  4.     _handle = CreateFileW(utf16Path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);  
  5. #else  
  6.     _handle = CreateFileA(path.c_str(), access, shareMode, NULL, creationDisp, flags, NULL);  
  7. #endif  

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

[cpp]  view plain copy
  1. Poco::Path file(filename); // filename 不是 utf16 编码  
  2. file.makeFile();  
  3. Poco::Path dest(_outDir, file); // _outDir 是 utf16 编码  
  4. dest.makeFile();  
  5. ...  
  6. Poco::FileOutputStream out(dest.toString()); // 这时候整个文件路径是不正确的  
  7. ZipInputStream inp(zipStream, hdr, false);  
  8. Poco::StreamCopier::copyStream(inp, out);  
  9. out.close();  

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

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

[cpp]  view plain copy
  1. std::string dest_filename = fileName;  
  2.   
  3. #if defined (POCO_WIN32_UTF8)  
  4. std::wstring utf16_name = s2ws(fileName);  
  5. UnicodeConverter::toUTF8(utf16_name, dest_filename);  
  6. #endif  
  7.   
  8. Poco::Path file(dest_filename);  

[cpp]  view plain copy
  1. std::wstring Decompress::s2ws(const std::string& s)  
  2. {  
  3.     setlocale(LC_ALL, "chs");   
  4.     const char* _Source = s.c_str();  
  5.     size_t _Dsize = s.size() + 1;  
  6.     wchar_t *_Dest = new wchar_t[_Dsize];  
  7.     wmemset(_Dest, 0, _Dsize);  
  8.     mbstowcs(_Dest,_Source,_Dsize);  
  9.     std::wstring result = _Dest;  
  10.     delete []_Dest;  
  11.     setlocale(LC_ALL, "C");  
  12.     return result;  
  13. }  

你可能感兴趣的:(开源)