Lazarus

Announcements => Third party => Topic started by: Tomxe on February 15, 2025, 09:15:11 am

Title: ZSTD compression/decompression for Lazarus, FPC
Post by: Tomxe on February 15, 2025, 09:15:11 am
License: MIT
https://github.com/Xelitan/ZSTD-compression-decompression-for-Delphi-Lazarus

Usage examples:
Code: Pascal  [Select][+][-]
  1. var F,P: TFileStream;
  2.     Size: Integer;
  3. begin
  4.   ZSTD_COMPRESSION := 20;
  5.   ZSTD_COMPRESSION := 1;
  6.  
  7.   F := TFileStream.Create('test.txt', fmOpenRead);
  8.   P := TFileStream.Create('test.zstd', fmCreate);
  9.   ZSTD(F, P);
  10.   Size := F.Size;
  11.   F.Free;
  12.   P.Free;
  13.  
  14.   F := TFileStream.Create('test.zstd', fmOpenRead);
  15.   P := TFileStream.Create('test2.txt', fmCreate);
  16.   UnZSTD(F, P, Size);
  17.   F.Free;
  18.   P.Free;
Title: Re: ZSTD compression/decompression for Lazarus
Post by: Thaddy on February 15, 2025, 10:25:02 am
Which one is it?
Code: Pascal  [Select][+][-]
  1. begin
  2.   ZSTD_COMPRESSION := 20;
  3.   ZSTD_COMPRESSION := 1;
Title: Re: ZSTD compression/decompression for Lazarus
Post by: Tomxe on February 15, 2025, 11:04:46 am
This is how you choose compression. Anything between 1 (fast, big file) and 20 (slow, small file) is fine. Max is 22 but is very slow. Negative values are also possible but files are just impractically big.
Title: Re: ZSTD compression/decompression for Lazarus
Post by: PascalDragon on February 15, 2025, 03:41:56 pm
This is how you choose compression. Anything between 1 (fast, big file) and 20 (slow, small file) is fine. Max is 22 but is very slow. Negative values are also possible but files are just impractically big.

It would be better if this would be part of the function call as a parameter as otherwise this isn't thread safe. I haven't checked whether you have other global state that would be problematic as well and in that case you should add functions to create/destroy a state object/record (which will also receive the compression setting) that is then passed to the compress and decompress functions.
Title: Re: ZSTD compression/decompression for Lazarus
Post by: Tomxe on February 15, 2025, 05:02:14 pm
The compression level as a global variable makes prettier code in case of compressing strings:
Code: Pascal  [Select][+][-]
  1. UnZSTD(ZSTD('Hello World'));
instead of:
Code: Pascal  [Select][+][-]
  1. UnZSTD(ZSTD('Hello World', 20));
But yes, you are right- thread safety needs to be fixed.
TinyPortal © 2005-2018