简体   繁体   English

用 ZLIB 压缩到字节数组?

[英]Compress with ZLIB to array of Bytes?

Can I somehow compress with Delphi using ZLIB (Deflate with ZLIB headers) and get an array of Bytes?我可以使用 ZLIB(使用 ZLIB 标头放气)以某种方式压缩 Delphi 并获得字节数组吗?

Right now I am copying from TMemoryStream but it would be nice to not copy back to array (so it's faster overall)现在我正在从 TMemoryStream 复制,但最好不要复制回数组(所以总体上更快)

  PackedStream := TMemoryStream.Create;
  ZLib := TZCompressionStream.Create(PackedStream);
  ZLib.WriteBuffer(UnpackedArray[0], UnpackedArrayLen);
  ZLib.Free;

  PackedArrayLen := PackedStream.Size;
  SetLength(PackedArray, PackedArrayLen);
  PackedStream.Position := 0;
  PackedStream.Read(PackedArray[0], PackedArrayLen);
  PackedStream.Free;

Simply use TMemoryStream.Memory as the byte array , just don't free the stream until you are done using the bytes:只需使用TMemoryStream.Memory作为字节数组,在使用完字节之前不要释放 stream:

PackedStream := TMemoryStream.Create;
try
  ZLib := TZCompressionStream.Create(PackedStream);
  try
    ZLib.WriteBuffer(UnpackedArray[0], UnpackedArrayLen);
  finally
    ZLib.Free;
  end;
  // use PackedStream.Memory up to PackedStream.Size bytes as needed...
finally
  PackedStream.Free;
end;

Otherwise, you can use TBytesStream instead of TMemoryStream :否则,您可以使用TBytesStream代替TMemoryStream

PackedStream := TBytesStream.Create;
try
  ZLib := TZCompressionStream.Create(PackedStream);
  try
    ZLib.WriteBuffer(UnpackedArray[0], UnpackedArrayLen);
  finally
    ZLib.Free;
  end;
  // use PackedStream.Bytes up to PackedStream.Size bytes as needed...
finally
  PackedStream.Free;
end;

Or, if you have a pre-allocated byte array, you can use TCustomMemoryStream giving it a pointer to that array so it will write directly into the array:或者,如果你有一个预先分配的字节数组,你可以使用TCustomMemoryStream给它一个指向该数组的指针,这样它就可以直接写入数组:

type
  TMemoryBufferStream = class(TCustomMemoryStream)
  public
    constructor Create(APtr: Pointer; ASize: NativeInt);
    function Write(const Buffer; Count: Longint): Longint; override;
  end;

constructor TMemoryBufferStream.Create(APtr: Pointer; ASize: NativeInt);
begin
  inherited Create;
  SetPointer(APtr, ASize);
end;

function TMemoryBufferStream.Write(const Buffer; Count: Longint): Longint;
var
  LAvailable: Int64;
  LNumToCopy: Longint;
begin
  Result := 0;
  LAvailable := Size - Position;
  if LAvailable > 0 then
  begin
    LNumToCopy := Count;
    if Int64(LNumToCopy) > LAvailable then
      LNumToCopy := Longint(LAvailable);
    if LNumToCopy > 0 then
    begin
      Move(Buffer, (PByte(Memory) + Position)^, LNumToCopy);
      Seek(LNumToCopy, soCurrent);
      Result := LNumToCopy;
    end;
  end;
end;
PackedStream := TMemoryBufferStream.Create(SomeBuffer, MaxBufferSize);
try
  ZLib := TZCompressionStream.Create(PackedStream);
  try
    ZLib.WriteBuffer(UnpackedArray[0], UnpackedArrayLen);
  finally
    ZLib.Free;
  end;
  // use SomeBuffer up to PackedStream.Size bytes as needed...
finally
  PackedStream.Free;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM