Tom
Tom

Reputation: 3044

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?

Right now I am copying from TMemoryStream but it would be nice to not copy back to array (so it's faster overall)

  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;

Upvotes: 1

Views: 614

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598329

Simply use TMemoryStream.Memory as the byte array, just don't free the stream until you are done using the bytes:

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:

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:

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;

Upvotes: 6

Related Questions