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

问题描述 投票:0回答:1

我正在尝试使用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上保存压缩数据?提前致谢。

delphi compression lzo
1个回答
3
投票

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

OutputLength := 0; // initialize only
...
try
  lzo2a_999_compress(FInput.Memory, FInput.Size, Buffer, OutputLength, WorkMem);
finally
  FOutput.CopyFrom(Buffer, OutputLength);
© www.soinside.com 2019 - 2024. All rights reserved.