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


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);
#endif

Poco::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;
}



猜你喜欢

转载自blog.csdn.net/kowity/article/details/7787890