我正在尝试使用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将是相同大小的缓冲区。如何将压缩的数据仅保存在OutputFile上?提前谢谢。
发布于 2018-03-06 19:21:27
在函数调用之前,您不能(也不需要)知道它。它是一个var参数,将由函数在返回时设置。然后,您可以使用OutputLength变量来知道要从缓冲区复制多少字节:
OutputLength := 0; // initialize only
...
try
lzo2a_999_compress(FInput.Memory, FInput.Size, Buffer, OutputLength, WorkMem);
finally
FOutput.CopyFrom(Buffer, OutputLength);https://stackoverflow.com/questions/49137884
复制相似问题