Recent

Author Topic: [paszlib]How to make streaming compression?  (Read 464 times)

Borneq

  • Full Member
  • ***
  • Posts: 238
[paszlib]How to make streaming compression?
« on: January 09, 2020, 08:41:15 am »
I use paszlib library, in zCompres unit is
Code: Pascal  [Select][+][-]
  1. function compress2 (dest : Pbyte;
  2.                     var destLen : cardinal;
  3.                     const source : array of byte;
  4.                     sourceLen : cardinal;
  5.                     level : integer) : integer;
  6. var
  7.   stream : z_stream;
  8.   err : integer;
  9. begin
  10.   stream.next_in := Pbyte(@source);
  11.   stream.avail_in := cardinal(sourceLen);
  12.   stream.next_out := dest;
  13.   stream.avail_out := cardinal(destLen);
  14. .....
  15.   err := deflateInit(stream, level);
  16. .....
  17.   err := deflate(stream, Z_FINISH);
  18.   if (err <> Z_STREAM_END) then
  19.   begin
  20.     deflateEnd(stream);
  21.     if err = Z_OK then
  22.       compress2 := Z_BUF_ERROR
  23.     else
  24.       compress2 := err;
  25.     exit;
  26.   end;
  27.   destLen := stream.total_out;
  28.  
  29.   err := deflateEnd(stream);
  30.   compress2 := err;
  31. end;
This compress one block at once, how to use TStream and compress/uncompress chunk by chunk. Unit gzIO has streaming but works on files given by path, I prefer TStream and maybe memory streams.
Is sggzip.pas but is not compatible with paszlib.
I want write method: at first it must deflateInit, at end deflateEnd, and in loop deflate(stream, ?); where "?" is not Z_FINISH and maybe one from list below:
Code: Pascal  [Select][+][-]
  1.    Z_NO_FLUSH      = 0;
  2.    Z_PARTIAL_FLUSH = 1;
  3.    Z_SYNC_FLUSH    = 2;
  4.    Z_FULL_FLUSH    = 3;
  5.    Z_FINISH        = 4;

rvk

  • Hero Member
  • *****
  • Posts: 4160
Re: [paszlib]How to make streaming compression?
« Reply #1 on: January 09, 2020, 10:17:48 am »
Maybe using the ZStream unit.

You could use Tcompressionstream (create with a destination stream) and use Tcompressionstream.write() to write chunks to that compression stream.

Or something like CompressionStream.CopyFrom(YourPreferredStream); should also work (because Tcompressionstream is a TStream descendant).



 

TinyPortal © 2005-2018