最近,我尝试将无损数据压缩集成到我的游戏引擎中以加载资产;但是这个简单的压缩示例似乎无法正常工作。有什么建议么 ?这是我的代码:
const char *srcData = "Hi ! This is a really really really long test string !";
const int dstBufferSize = LZ4_compressBound(sizeof(srcData));
char *dstData = new char[dstBufferSize];
int bytesPassed = LZ4_compress_default(srcData, dstData,
sizeof(srcData),
dstBufferSize); // compress data
BOOST_LOG_TRIVIAL(info) << dstData << std::endl; // print compressed data
delete[] dstData;
这是输出。显然,您可以看到它是错误的(缺少部分字符串):
[2016-02-24 15:56:47.986366] [0x00000b0c] [info] @Hi !═══════════════²²²²À▀WÏÇ0
编辑 解压缩数据时,仅出现“Hi”部分:其余为随机字符/无字符
编辑 2根据西蒙的建议,我更改了代码;但解压代码后;我只得到你好!(之后什么都没有);这是更新的代码:
const char *srcData = "Hi ! This is a really really really long test string !";
const int dstBufferSize = LZ4_compressBound(strlen(srcData) + 1);
char *dstData = new char[dstBufferSize];
int bytesPassed = LZ4_compress_default(srcData, dstData,
sizeof(srcData),
dstBufferSize);
BOOST_LOG_TRIVIAL(info) << dstData << std::endl;
std::ofstream fWriter("test.bin", std::ofstream::binary);
fWriter << dstData;
fWriter.close();
char* decStr = new char[strlen(srcData) + 1];
LZ4_decompress_fast(dstData, decStr, strlen(srcData) + 1);
std::cout << decStr << std::endl; // only Hi appearing
delete[] dstData;