我想解压缩二进制数据中的 LZ4 原始流。我阅读了文档并提出了以下功能。根据代码,它应该继续解压缩,直到用完压缩数据,但事实并非如此。我究竟做错了什么?另外,我想为压缩大小添加一个 writeln。
function Try(inStream: TMemorySTream): Boolean;
var
i, j, k: int64;
orig_buff: Pansichar;
compressed_buff: Pansichar;
lz4StreamDecode: PLZ4_streamDecode_t;
begin
result := false;
lz4StreamDecode := LZ4_createStreamDecode();
k := 0;
while True do
begin
orig_buff := allocmem(100);
compressed_buff := allocmem(4064);
j := inStream.Read(orig_buff^, 100);
i := lz4.LZ4_decompress_safe_continue(lz4StreamDecode, orig_buff,
compressed_buff, j, 4064);
Freemem(orig_buff);
Freemem(compressed_buff);
if i <= 0 then
break;
inc(k);
end;
if k <> 0 then
result := True;
LZ4_freeStreamDecode(lz4StreamDecode);
end;
编辑1:删除了释放输出缓冲区(临时缓冲区)的部分,问题仍然存在,而不是解压缩整个流然后退出,它只是事先退出循环。
function Try(inStream: TMemorySTream): Boolean;
var
i, j, k: int64;
orig_buff: Pansichar;
compressed_buff: Pansichar;
lz4StreamDecode: PLZ4_streamDecode_t;
begin
result := false;
lz4StreamDecode := LZ4_createStreamDecode();
compressed_buff := allocmem(64 * 1024 * 1024);
k := 0;
while True do
begin
orig_buff := allocmem(100);
j := inStream.Read(orig_buff^, 100);
i := lz4.LZ4_decompress_safe_continue(lz4StreamDecode, orig_buff,
compressed_buff, j, (64 * 1024 * 1024));
Freemem(orig_buff);
if i <= 0 then
break;
inc(k);
end;
if k <> 0 then
result := True;
Freemem(compressed_buff);
LZ4_freeStreamDecode(lz4StreamDecode);
end;