Cross-platform Unzip File Support in C++

Hi,

I need to integrate support for consuming third-party content in the application I’m developing in the form of zip files. In particular, the app is communicating with a third-party API that allows downloading of assets in the form of zip. I need to unzip those files and load the data programmatically. The whole process is developed and orchestrated in C++, so I will need to implement the unzipping functionality as part of the wider framework for content consumption. Unfortunately, packing them as pak files instead is not an option for this, as there isn’t control over the API. Also, I need the unzipping functionality to be supported on both Windows and iOS (as the app is intended for iOS). What would be some recommended solutions for this? I have already tried using the built in functionality (as attached in the replicate area), but with not much success.

Thanks in advance.

Lefteris

Steps to Reproduce
`TArray ZipData;
if (!FFileHelper::LoadFileToArray(ZipData, *FilePath))
{
Debug::LogError(FString::Printf(TEXT(“Failed to load zip file: %s”), *FilePath));
return;
}

try
{
TArray UncompressedData;
UncompressedData.SetNum(ZipData.Num() * 4);

const bool bResult = FCompression::UncompressMemory(
NAME_Zlib,
UncompressedData.GetData(),
UncompressedData.Num(),
ZipData.GetData(),
ZipData.Num()
);

if (bResult)
{
Debug::LogSuccess(“Successfully extracted zip file.”);
}
else
{
Debug::LogError(“Failed to extract zip file.”);
}
}
catch (std::exception& e)
{
Debug::LogError(FString::Printf(TEXT(“Error while extracting; %s”), *FString{e.what()}));
}`

Hi,

If you have a .zip file containing several other files, you cannot uses the FCompression API directly. If you search Google about decompressing zip with zlib, you will get a nice AI summary explaining why. Here the one I got:

[Image Removed]So, FCompression doesn’t know about the zip file format (header, footer, metadata). Instead, you need to use something like FZipArchiveReader (\Engine\Source\Developer\FileUtilities\Private\ZipArchiveReader.cpp). This uses the libzip library from Engine\Source\ThirdParty\libzip\1.9.2\ (https://libzip.org/). The problem is that FZipArchiveReader is only available from the Editor. You could port FZipArchiveReader and libzip to work on the platforms you are targeting. On Windows, the port is trivial… it is already working in the Editor. On iOS, you might want to check for a native iOS API, find an iOS port of libzip already available or just port the version coming with the Engine. It will requires some work. Note that we have a version ported on Mac (D:\UE_5.5\Engine\Source\ThirdParty\libzip\1.9.2\lib\Mac\Release\libzip.a), so porting to iOS is probably almost there.

Regards,

Patrick

Hi Patrick, many thanks for your swift response. I ended up using miniz as a third party library in the application, which worked on all three (Windows, macOS, iOS). Many thanks for your help.

Hi,

You are welcome, I’m glad you got it working!

Regards,

Patrick