FCompression是一个用来对内存数据进行压缩解压的结构体(类),提供了几个静态方法,底层基于zlib库,支持zlib和gzib格式的压缩解压(gzib部分支持)。另外还有个Oodle格式,不过引擎没有集成。
FCompression 主要有三个方法,CompressMemoryBound用来预估压缩需要的缓存空间,CompressMemory用来压缩,UncompressMemory用来解压。在压缩期间,需要的缓存大小可以提前确定,但是解压所需的缓存无法确定。 UncompressMemory 需要一个解压缓存的大小UncompressedSize ,而这个大小要是解压后即数据未压缩之前的大小,如果传入的size和解压后的size不一样,会打印警告并返回错误,但是实际上数据已经解压成功了。所以使用这个函数最好在数据头里面将数据的原始尺寸写进去。
/**
* Thread-safe abstract compression routine to query memory requirements for a compression operation.
*
* @param Flags Flags to control what method to use and optionally control memory vs speed
* @param UncompressedSize Size of uncompressed data in bytes
* @param BitWindow Bit window to use in compression
* @return The maximum possible bytes needed for compression of data buffer of size UncompressedSize
*/
CORE_API static int32 CompressMemoryBound(FName FormatName, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
/**
* Thread-safe abstract compression routine. Compresses memory from uncompressed buffer and writes it to compressed
* buffer. Updates CompressedSize with size of compressed data. Compression controlled by the passed in flags.
*
* @param Flags Flags to control what method to use and optionally control memory vs speed
* @param CompressedBuffer Buffer compressed data is going to be written to
* @param CompressedSize [in/out] Size of CompressedBuffer, at exit will be size of compressed data
* @param UncompressedBuffer Buffer containing uncompressed data
* @param UncompressedSize Size of uncompressed data in bytes
* @param BitWindow Bit window to use in compression
* @return true if compression succeeds, false if it fails because CompressedBuffer was too small or other reasons
*/
CORE_API static bool CompressMemory(FName FormatName, void* CompressedBuffer, int32& CompressedSize, const void* UncompressedBuffer, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
/**
* Thread-safe abstract decompression routine. Uncompresses memory from compressed buffer and writes it to uncompressed
* buffer. UncompressedSize is expected to be the exact size of the data after decompression.
*
* @param Flags Flags to control what method to use to decompress
* @param UncompressedBuffer Buffer containing uncompressed data
* @param UncompressedSize Size of uncompressed data in bytes
* @param CompressedBuffer Buffer compressed data is going to be read from
* @param CompressedSize Size of CompressedBuffer data in bytes
* @param bIsSourcePadded Whether the source memory is padded with a full cache line at the end
* @return true if compression succeeds, false if it fails because CompressedBuffer was too small or other reasons
*/
CORE_API static bool UncompressMemory(FName FormatName, void* UncompressedBuffer, int32 UncompressedSize, const void* CompressedBuffer, int32 CompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
示例代码
char* source = "FCompression Compress Test....................................................";
int srcLen = strlen(source) + 1;
int compressBufferLen = FCompression::CompressMemoryBound(NAME_Zlib, srcLen);
void* compressBuffer = FMemory::Malloc(compressBufferLen);
bool success = FCompression::CompressMemory(NAME_Zlib, compressBuffer, compressBufferLen, source, srcLen);
void* uncompressBuffer = FMemory::Malloc(1024);
FMemory::Memzero(uncompressBuffer, 1024);
success = FCompression::UncompressMemory(NAME_Zlib, uncompressBuffer, srcLen, compressBuffer, compressBufferLen);
FMemory::Free(uncompressBuffer);
FMemory::Free(compressBuffer);