Recent

Author Topic: ZSTD compression/decompression for Lazarus, FPC  (Read 464 times)

Tomxe

  • New Member
  • *
  • Posts: 40
ZSTD compression/decompression for Lazarus, FPC
« 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;
« Last Edit: February 16, 2025, 11:41:13 am by Tomxe »

Thaddy

  • Hero Member
  • *****
  • Posts: 16672
  • Kallstadt seems a good place to evict Trump to.
Re: ZSTD compression/decompression for Lazarus
« Reply #1 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;
« Last Edit: February 15, 2025, 11:25:52 am by Thaddy »
But I am sure they don't want the Trumps back...

Tomxe

  • New Member
  • *
  • Posts: 40
Re: ZSTD compression/decompression for Lazarus
« Reply #2 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.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5909
  • Compiler Developer
Re: ZSTD compression/decompression for Lazarus
« Reply #3 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.

Tomxe

  • New Member
  • *
  • Posts: 40
Re: ZSTD compression/decompression for Lazarus
« Reply #4 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