LZO压缩 - 如何设置目标长度?

时间:2018-03-06 18:42:48

标签: delphi compression lzo

我正在尝试使用lzo.dll来压缩某些文件,我的代码(Delphi)就是:

function lzo2a_999_compress(const Source: Pointer; SourceLength: LongWord; Dest: Pointer; var DestLength: LongWord; WorkMem: Pointer): Integer; cdecl; external 'lzo.dll';
...
function LZO_compress(FileInput, FileOutput: String): Integer;
var
   FInput, FOutput: TMemoryStream;
   WorkMem: Pointer;
   Buffer: TBytes;
   OutputLength: LongWord;
begin
   FInput := TMemoryStream.Create;
   FOutput := TMemoryStream.Create;
   FInput.LoadFromFile(FileInput);
   FInput.Position := 0;
   GetMem(WorkMem, 1000000);
   OutputLength := ??!?!?!;
   SetLength(Buffer, OutputLength);
   try
      lzo2a_999_compress(FInput.Memory, FInput.Size, Buffer, OutputLength, WorkMem);
   finally
      FOutput.CopyFrom(Buffer, Length(Buffer));
   end;
   FOutput.SaveToFile(FileOutput);
   FreeMem(WorkMem, 1000000);
   FInput.Free;
   FOutput.Free;
end;
...

问题是:如何设置“OutputLength”?我可以为防止出现问题而调整大小,但FOutput的大小与Buffer相同。如何只在OutputFile上保存压缩数据? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

在函数调用之前,您不能(也不必)知道它。它是var参数,将在返回时由函数设置。然后,您可以使用OutputLength变量来了解要从缓冲区复制的字节数:

OutputLength := 0; // initialize only
...
try
  lzo2a_999_compress(FInput.Memory, FInput.Size, Buffer, OutputLength, WorkMem);
finally
  FOutput.CopyFrom(Buffer, OutputLength);