So i was trying to compress a zip file which contains japanese characters filenames in cpp with minizip library, but no matter what i do it does not seem to work and i see a random set of characters instead when open it via WinRAR or 7-zip.
I tried to convert std::wstring file paths to utf8 std::string
I tried to to find tutorials that can handle it.
Note:i am using std::wstring for paths and filenames.
#include ...#include "UniCoder.h" //to import ConvertWideToUtf8(std::wstring);using namespace std;bool zipper_add_dir(zipFile zfile, wstring wdirname) { size_t len; string dirname = ConvertWideToUtf8(wdirname);//is the problem here? int ret; if (zfile == NULL || wdirname.c_str() == NULL || *wdirname.c_str() == '\0') return false; ret = zipOpenNewFileInZip(zfile, wdirname.c_str(), NULL, NULL, 0, NULL, 0, NULL, 0, 0);//or here? if (ret != ZIP_OK) return false; zipCloseFileInZip(zfile); return ret == ZIP_OK ? true : false;}int CreateZipFile(vector<wstring> paths, wstring originPath, wstring wdestinationPath) { zlib_filefunc_def ffunc; string destinationPath = ConvertWideToUtf8(wdestinationPath); zipFile zf = zipOpen(destinationPath.c_str(), APPEND_STATUS_CREATE); if (zf == NULL) return 1; bool _return = true; for (size_t i = 0; i < paths.size(); i++) { fstream file(paths[i].c_str(), ios::binary | ios::in); if (file.is_open()) { file.seekg(0, ios::end); long size = file.tellg(); file.seekg(0, ios::beg); vector<char> buffer(size); if (size == 0 || file.read(&buffer[0], size)) { zip_fileinfo zfi = { 0 }; wstring wfileName = paths[i].substr(paths[i].rfind('\\') + 1); string fileName = ConvertWideToUtf8(wfileName); if (Z_OK == zipOpenNewFileInZip(zf, fileName.c_str()/*or is it here*/, &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION)) { if (zipWriteInFileInZip(zf, size == 0 ? "" : &buffer[0], size)) _return = false; if (zipCloseFileInZip(zf)) _return = false; file.close(); continue; } } file.close(); } else { TCHAR fuDir[MAX_PATH]; TCHAR srDir[MAX_PATH]; TCHAR reDir[MAX_PATH];//does using TCHAR do any difference? StringCchCopy(fuDir, MAX_PATH, paths[i].c_str()); StringCchCopy(srDir, MAX_PATH, originPath.c_str()); StringCchCat(srDir, MAX_PATH, TEXT("\\")); int cur = 0; ZeroMemory(reDir, MAX_PATH); for (int j = 0; j < MAX_PATH; j++) { if (fuDir[j] != srDir[j]) { reDir[cur] = fuDir[j]; cur++; } } StringCchCat(reDir, MAX_PATH, TEXT("\\")); zipper_add_dir(zf, reDir); } _return = false; } if (zipClose(zf, NULL)) return 3; if (!_return) return 4; return Z_OK;}
a file named おはも.txt when compressed i get ÒüèÒü»Òéé.txt when i set std::wstring to convert into utf8 std::string
The code for ConvertWideToUtf8:
string ConvertWideToUtf8(const wstring& wstr) { int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL); string str(count, 0); WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL); return str; }