Hello,
I need to handle zip files in my code and use a cpp library to do so (ZipLib).
My code looks like this:
FString FileFilter = "*";
// get content
TArray<FString> DirContent;
IFileManager::Get().FindFilesRecursive(DirContent, *ThisGS->GetYagModulesDir(), *FileFilter, true, false);
// +1 because of the last "/"
int32 ModuleDirNbOfChar = AYagGameState::YagModulesDir.Len() + 1;
// filling the zip
for (int i = 0; i < DirContent.Num(); i++)
{
// source
const FString FileOnDisk = DirContent*;
const std::string FileOnDiskStdStr = std::string(TCHAR_TO_UTF8(*FileOnDisk));
// target
const FString FileInArchive = FileOnDisk.RightChop(ModuleDirNbOfChar);
const std::string FileInArchiveStdStr = std::string(TCHAR_TO_UTF8(*FileInArchive));
//GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("ZipModule file: %s"), *FString(FileOnDiskStdStr.c_str())));
ZipFile::AddFile(ZipPathStdStr, FileOnDiskStdStr, FileInArchiveStdStr);
}
ZipLib needs std::string as an input.
This code works well when the file name doesn’t contain special characters.
But as soon as there are files with special character, like so:
I get a crash because the file is not found and can’t be opened:
And it can’t be opened because its name is not read correctly:
Now, std::string(TCHAR_TO_UTF8(*FileOnDisk)) seems to be the standard way to convert an FString to std:string in UE4 (it comes from a Rama tutorial and is the standard answer found on google), but it doesn’t seem to work well with windows file name FStrings.
If i enable the debug message here, i get a wrong file name also: “hé hé” becomes “h?? h??” (and using TCHAR_TO_ANSI gives “h? h?”, so each “?” is one wrongly encoded byte apparently).
Any idea of how to handle that ?
As always, i ask in case anyone knows, and will post the answer if i find it.
Thanks
Cedric