Zstandard for Lazarus, "without" DLL, Windows only lazarus / 21-General ---------------------------------------------------------------------- domasz · 2024-02-26 00:02 I prefer single EXE programs but there is no ZStandard implementation in Pascal. So here's my solution for Windows 64 bit, a bit ugly but: 1) no DLL file to distribute 2) antivirus programs don't complain- you can check on virustotal 3) easy to update Zstandard to newest release 4) easy to adapt code for Win32, Linux, MacOS. It just extracts the DLL to the temporary folder and uses it from there. The DLL is UPX packed to be smaller. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ZSTDLib, ZSTD Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const BufSize = 40960000;var InF, OutF: TFileStream;    Z: TZSTDCompressStream;    Len: Integer;    Buf: array of Byte;    Options: TZSTDCompressOptions;     c1, c2, f: Int64;begin  ZSTDDllName := GetTempDir + 'libzstd-117788.dll';   if not FileExists(ZSTDDllName) then begin    OutF := TFileStream.Create(ZSTDDllName, fmCreate);    OutF.Write(ZStdBody, Length(ZStdBody));    OutF.Free;  end;   QueryPerformanceFrequency(f);  QueryPerformanceCounter(c1);   InF := TFileStream.Create('whatever.input', fmOpenRead);   OutF := TFileStream.Create('pack.zstd', fmCreate);   Options.Init;  //Options.CompressionLevel := 100; //max  Options.CompressionLevel := 2; //fast   Z := TZSTDCompressStream.Create(OutF, Options);   SetLength(Buf, BufSize);   while InF.Position < InF.size do begin    Len := InF.Read(Buf[0], BufSize);     Z.Write(Buf[0], Len);    //OutF.Write(Buf[0], Len);  end;   Z.Free;  InF.Free;  OutF.Free;  //DeleteFile(ZSTDDllName);   QueryPerformanceCounter(c2);  ShowMessage(Format('%f sec', [ (c2-c1)/f ])); ---------------------------------------------------------------------- Fibonacci · 2024-02-26 00:12 You know you can load a DLL directly from memory without saving it to disk? It aint easy though. You need to manually do the reclocations and patch the IAT. But no need to do that, you can static link this lib with {$linklib xxx.a}. ---------------------------------------------------------------------- domasz · 2024-02-26 00:28 Quote from: Fibonacci on February 26, 2024, 01:12:47 amYou know you can load a DLL directly from memory without saving it to disk? It aint easy though. You need to manually do the reclocations and patch the IAT. Yes, but antivirus programs don't like it and I read its problematic in case of 64 bit Windows. Quote from: Fibonacci on February 26, 2024, 01:12:47 amBut no need to do that, you can static link this lib with {$linklib xxx.a}. I can static link this DLL into EXE? I think I need a special .OBJ file or something to static link. ---------------------------------------------------------------------- Fibonacci · 2024-02-26 00:55 You need a static library libzstd.a I found 4 such files on my PC, from MinGW installation or Ruby installation, I dont know where it is available for download, I looked at msys and at github repo and there was no static library for win64. ---------------------------------------------------------------------- domasz · 2024-02-26 00:59 Yes, I would need a binary in very specific format which nobody provides. And not just an .A file because this extension is used by different things. That's the same problem with most libraries. There is ZStandard with static binaries for Delphi but they don't work in Lazarus. So my method is the only one working. ---------------------------------------------------------------------- Fibonacci · 2024-02-26 01:13 EDIT: My bad, something went wrong, BRB! ---------------------------------------------------------------------- domasz · 2024-02-26 01:17 Quote from: Fibonacci on February 26, 2024, 02:13:30 amHere you go, ready to compile Archive includes libstd.a That's great, thank you! Only would be nice to know where the libstd.a comes from (to get newer ones in the future) ---------------------------------------------------------------------- Fibonacci · 2024-02-26 01:18 Wait, I have NO IDEA how that was possible but the file loaded libzstd.dll and it worked Investigating. ---------------------------------------------------------------------- Fibonacci · 2024-02-26 01:43 Ok, dunno how that happened, that Pascal source, although I uncommented ifdef to static link the lib, it still used a DLL, and it happens I have a libzstd.dll available in my PATH, so it worked. I posted that project here, but then checked the binary file and there were imports from libzstd.dll. Here is the fixed version, I removed everything that wanted to load a DLL. More static libs were needed. {$linklib libzstd.a} {$linklib libgcc.a} {$linklib libkernel32.a} {$linklib libucrt.a} Archive has ~3.5 MB so I cant upload it here on the forum, so the link is here: https://github.com/fibodevy/zflate/files/14398693/fpc-zstd-static.zip (I dropped the file on a new issue on github and closed the tab, so I dont know how long the link will stay valid ) ---------------------------------------------------------------------- 440bx · 2024-02-26 01:58 What I'm going to say  isn't about libzstd but someone may be interested in knowing that Windows NTDLL provides compression and decompression functions.   While they aren't as "effective" as e.g, 7Zip or rar, they are quite decent and no need to distribute a dll (if ntdll is missing, the user has more important problems than decompressing a file... ) I thought I'd mention that in case it might be of interest to someone. ---------------------------------------------------------------------- KodeZwerg · 2024-02-26 02:00 Do by accident you maybe also got a working version for static lz4 / lz4hc with fpc header flying around? ---------------------------------------------------------------------- Fibonacci · 2024-02-26 02:08 Quote from: 440bx on February 26, 2024, 02:58:37 amWhat I'm going to say  isn't about libzstd but someone may be interested in knowing that Windows NTDLL provides compression and decompression functions. These APIs are: - compression: RtlGetCompressionWorkSpaceSize() -> RtlCompressBuffer() - decompression: RtlGetCompressionWorkSpaceSize() -> RtlDecompressBuffer() Compression ratio is really low IIRC. Quote from: KodeZwerg on February 26, 2024, 03:00:10 amDo by accident you maybe also got a working version for static lz4 / lz4hc with fpc header flying around? Just static libs and C headers ---------------------------------------------------------------------- 440bx · 2024-02-26 02:17 Quote from: Fibonacci on February 26, 2024, 03:08:48 amCompression ratio is really low IIRC. yes, it is relatively low but, I'd say it's decent. That said, definitely not nearly as good as what is obtained from more sophisticated algorithms used in 7zip and Winrar. The really nice feature is: no external dll needed because the routines are part of the O/S.  That's a nice plus. ---------------------------------------------------------------------- KodeZwerg · 2024-02-26 04:07 Quote from: Fibonacci on February 26, 2024, 03:08:48 amJust static libs and C headers Many thanks, will try to do same like you've done with Zstandard and upload when done or cry for help when it fail ---------------------------------------------------------------------- domasz · 2024-02-26 07:39 Quote from: Fibonacci on February 26, 2024, 02:43:35 am {$linklib libzstd.a} {$linklib libgcc.a} {$linklib libkernel32.a} {$linklib libucrt.a} Where are these files from? You compiled them by yourself? I found good files (except libzstd.a) in MinGW release (winlibs-x86_64-posix-seh- gcc-13.2.0-llvm-17.0.6-mingw-w64ucrt-11.0.1-r5.7z). ---------------------------------------------------------------------- marcov · 2024-02-26 08:04 the next major Windows 11 update is going to provide tar, 7-zip, rar, gz. Let's hope it is also in API form :-) https://blogs.windows.com/windowsdeveloper/2023/05/23/bringing-the- power-of-ai-to-windows-11-unlocking-a-new-era-of-productivity-for- customers-and-developers-with-windows-copilot-and-dev-home/ https://www.bleepingcomputer.com/news/microsoft/windows-11-getting- native-support-for-7-zip-rar-and-gz-archives/ ---------------------------------------------------------------------- Fibonacci · 2024-02-26 08:06 Quote from: domasz on February 26, 2024, 08:39:10 amQuote from: Fibonacci on February 26, 2024, 02:43:35 am {$linklib libzstd.a} {$linklib libgcc.a} {$linklib libkernel32.a} {$linklib libucrt.a} Where are these files from? You compiled them by yourself? I found good files (except libzstd.a) in MinGW release (winlibs-x86_64-posix-seh- gcc-13.2.0-llvm-17.0.6-mingw-w64ucrt-11.0.1-r5.7z). Yep, they are from https://www.mingw-w64.org/downloads/ and libzstd.a like I said I found in Ruby installation and in some old MinGW installation ---------------------------------------------------------------------- domasz · 2024-02-26 11:45 Here are good .A files from known places: http://diecastia.com/_lazarus/zstd_bin.zip winlibs-x86_64-posix-seh- gcc-13.2.0-llvm-17.0.6-mingw-w64ucrt-11.0.1-r5.7z mingw-w64-x86_64-zstd-1.4.4-1-any.pkg.tar.xz This is LibZSTD version 1.4.4. Not the newest but starting with version 1.4.5 MinGW comes with .A file which requires .DLL. ---------------------------------------------------------------------- paule32 · 2024-02-26 11:55 For Information only: To compress a .DLL which should be included into .EXE, You can use the Free available .ExE, and .DLL - Packer UPX. UPX comes with Source Code, so you can inspect the implementation. You can download UPX on sourceforge.net. My experiences are, that you can make a 2.500 Byte compressed EXE File from a 5.100 Byte Exe File. Also near a ratio of 2.2 ---------------------------------------------------------------------- rvk · 2024-02-26 11:59 Quote from: paule32 on February 26, 2024, 12:55:44 pmMy experiences are, that you can make a 2.500 Byte compressed EXE File from a 5.100 Byte Exe File.With half of the worlds virusscanners tripping over this ---------------------------------------------------------------------- paule32 · 2024-02-26 12:07 yeah. But you can add a Exception in the Anti-Virus-Setting's. For me, I use Avast - it check's all new binary Images that are accessed in/to Memory. If you have created (Avast) a Hash of the binary, the Anti-Virus- Software (Avast) will remember this, and don't pop out/prompt the User with a Message. Sure, you could do this only for trusted Users (which would be with 100% knowing the Developers of the Original Image). But, when you are an own Entrepreneur, your Hard Disk Store will say to you: Thank you. Sure, you need good Memory Sticks plugged into you Computer Sockets - because the binaries will be loaded into Memory before they will be execute... ---------------------------------------------------------------------- domasz · 2024-02-26 12:35 I already upxed the DLL before embedding it and on virustotal only 1 antivirus said it's "malicious". All others were fine with it. The same method but DLL as a resource - 2 antivirus said it's "malicious". And for LZ4 I made a new post so people can find it: https://forum.lazarus.freepascal.org/index.php/topic,66391.0.html ---------------------------------------------------------------------- KodeZwerg · 2024-02-26 12:37 The major antivirus scanners have UPX decompression included while I did prefer in past different executable packers, PECompact and ASPack. ---------------------------------------------------------------------- marcov · 2024-02-26 12:47 FPC used UPX a while to compress all binaries, and it was a never ending source of trouble. Antivirus, but also some PE functionality simply not or faulty implemented, trouble with EXE programs that provide traces, resource hackers etc etc. UPX Antivirus trouble comes in waves. It might be ok for a while, but if you do it for a decade, you will have seen several flare ups. And in practice all people will put the few MBs of EXEs on a 100GB HD, SSD or flash card anyway. Not worth the trouble. In the end we simply removed the upx-after-build support from the buildsystem. It was a remnant of pre-2000 MSDOS times with 240MB harddisks anyway. The few people doing retro work can UPX it themselves. Quote from: paule32 on February 26, 2024, 01:07:47 pmyeah. But you can add a Exception in the Anti-Virus-Setting's. Or spend 2 hours with a customer on the phone trying to convince him that you don't work for CIA because "you sent him a virus". And that was 15 years ago, nowadays paranoia and conspiracy theories have been dialed up significantly compared to then. ---------------------------------------------------------------------- Fibonacci · 2024-02-26 13:01 Quote from: domasz on February 26, 2024, 12:45:12 pmThis is LibZSTD version 1.4.4. Not the newest but starting with version 1.4.5 MinGW comes with .A file which requires .DLL. By doing writeln(ZSTD_versionString); you can see the version in my ZIP is 1.5.0, little newer. Edit: Download newest zstd from official repo: https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-v1.5.5- win64.zip go to static, get file libzstd_static.lib, change the name to libzstd.a And now you have most recent 1.5.5 version ---------------------------------------------------------------------- domasz · 2024-02-26 13:05 Quote from: Fibonacci on February 26, 2024, 02:01:54 pmBy doing writeln(ZSTD_versionString); you can see the version in my ZIP is 1.5.0, little newer. Yes, but you don't know where exactly that file came from so it's not a good idea to use it. ---------------------------------------------------------------------- Fibonacci · 2024-02-26 13:07 Quote from: domasz on February 26, 2024, 02:05:51 pmYes, but you don't know where exactly that file came from so it's not a good idea to use it. Thats rational. But you know what? You included a binary file in the .pas, who knows whats that ---------------------------------------------------------------------- domasz · 2024-02-26 13:14 Quote from: Fibonacci on February 26, 2024, 02:07:37 pmQuote from: domasz on February 26, 2024, 02:05:51 pmYes, but you don't know where exactly that file came from so it's not a good idea to use it. Thats rational. But you know what? You included a binary file in the .pas, who knows whats that Hah It's official DLL but UPXed so anyone can verify Your method worked, thanks! I tried before: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$linklib libzstd_static.lib}and it didn't work because of wrong header. After renaming the file this works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$linklib libzstd.a} ---------------------------------------------------------------------- domasz · 2025-03-08 18:03 How did you know to include these particular files? {$linklib libgcc.a} {$linklib libkernel32.a} {$linklib libucrt.a} ---------------------------------------------------------------------- Fibonacci · 2025-03-08 18:13 FPC told me which functions were missing, then I simply searched for them in the .a files using grep ---------------------------------------------------------------------- domasz · 2025-03-08 18:21 Great, thanks! ---------------------------------------------------------------------- paule32 · 2025-03-08 18:30 As far as I know, grep is for text files... But yes, you can use grep to search the file of String Matches. But for:  grep your_regex_pattern -n file.txt grep output the line number of the Match. I am a little bit the Fan of minimal Executables. But FPC is very fast for creating them. *.a Archive Files provide you a import.a file which contains the Procedures and Functions with a import Link to the DLL. Windows itself use DLL's to save Memory. And for me, it makes no sense, to store 100 MB Executables on the Computer disk. It makes me more Sense to save Memory and Disk Space by using DLL files FPC create a import library .a File, which can be used for second using when the Project is a Library. I find it very intressting to pack the DLL and EXE Files. Because the normal user of a Computer Desktop tends to be install all stuff that comes from a monthly Paper and CD. Then the Computer hard disk is overswapped with Tools, that use the same Functions and Procedures... I think it is Time to open a FPC Store, where the trusted Application's can be download after a check of Viruses and Trojan... But this is my point of mind... others: please make your own points of mind... My Problems are not of others... others Problems are not mine, too ... ---------------------------------------------------------------------- domasz · 2025-03-08 19:23 Linux guys like to store libraries in system directory. The result is- you can't just copy an app to another computer because dependencies are missing. So I think embedding everything in EXE, or at least app's dir, is the best approach. Users don't need to care about dependencies. And even a 100 MB EXE today is pretty much nothing. For an SSD drive that's instant loading. ---------------------------------------------------------------------- msintle · 2025-03-09 15:29 Quote from: domasz on March 08, 2025, 08:23:12 pmLinux guys like to store libraries in system directory. The result is- you can't just copy an app to another computer because dependencies are missing. Or you can use InstallAware for Linux and copy the libraries you need over to the system directory in an automatically elevated setup. ---------------------------------------------------------------------- Fibonacci · 2025-05-27 14:58 Quote from: 440bx on February 26, 2024, 03:17:26 amQuote from: Fibonacci on February 26, 2024, 03:08:48 amCompression ratio is really low IIRC. yes, it is relatively low but, I'd say it's decent. That said, definitely not nearly as good as what is obtained from more sophisticated algorithms used in 7zip and Winrar. The really nice feature is: no external dll needed because the routines are part of the O/S.  That's a nice plus. Found a new API. Introduced in Windows 10 1511. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program app; uses SysUtils, Windows, Classes; type  PD3D_SHADER_DATA = ^D3D_SHADER_DATA;  D3D_SHADER_DATA = record    pBytecode: Pointer;    BytecodeLength: SIZE_T;  end; type  PID3DBlob = ^ID3DBlob;  ID3DBlob = interface(IUnknown)    ['{8BA5FB08-5195-40e2-AC58-0D989C3A0102}']    function GetBufferPointer: Pointer; stdcall;    function GetBufferSize: SIZE_T; stdcall;  end; const  D3D_COMPRESS_SHADER_KEEP_ALL_PARTS = $00000001; function D3DCompressShaders(  uNumShaders: UINT;  pShaderData: PD3D_SHADER_DATA;  uFlags: UINT;  out ppCompressedData: ID3DBlob): HRESULT; stdcall; external 'D3DCompiler_47.dll'; function D3DDecompressShaders(  pSrcData: Pointer;  SrcDataSize: SIZE_T;  uNumShaders: UINT;  uStartIndex: UINT;  pIndices: PUINT;  uFlags: UINT;  ppShaders: PID3DBlob;  pTotalShaders: PUINT): HRESULT; stdcall; external 'D3DCompiler_47.dll'; function getfile(p: string): string;var  m: TMemoryStream;begin  m := TMemoryStream.Create;  m.LoadFromFile(p);  setlength(result, m.Size);  m.Read(result[1], m.Size);  m.Free;end; procedure main;var  dsa: D3D_SHADER_DATA;  blob: ID3DBlob;  s, compressed: string;begin  s := getfile('C:\Windows\explorer.exe');  writeln('uncompressed len = ', length(s));   dsa.pBytecode := @s[1];  dsa.BytecodeLength := length(s);   if D3DCompressShaders(1, @dsa, D3D_COMPRESS_SHADER_KEEP_ALL_PARTS, blob)  <> S_OK then begin    writeln('fail compress');    exit;  end;   writeln('compressed len   = ', blob.GetBufferSize);   setlength(compressed, blob.GetBufferSize);  move(blob.GetBufferPointer^, compressed[1], length(compressed));   if D3DDecompressShaders(@compressed[1], length(compressed), 1, 0, nil, 0, @blob, nil) <> S_OK then begin    writeln('fail decompress');    exit;  end;   writeln('decompressed len = ', blob.GetBufferSize);  writeln('decompress ok? ', CompareMem(blob.GetBufferPointer, @s[1], length(s)));  writeln('compressed to ', Round((length(compressed)/length(s))*100), '% of the original size');end; begin  try    main;  finally    readln;  end;end. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uncompressed len = 5973416compressed len   = 2096961decompressed len = 5973416decompress ok? TRUEcompressed to 35% of the original size Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i explorer.exe        5834 KBD3DCompressShaders: 2048 KBZIP Ultra:        2567 KB7zip Ultra:         1875 KB ---------------------------------------------------------------------- gues1 · 2025-05-27 16:10 Quote from: Fibonacci on May 27, 2025, 04:58:09 pmFound a new API. Introduced in Windows 10 1511. https://learn.microsoft.com/en- us/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dcompressshaders QuoteNote  You can use this API to develop your Windows Store apps, but you can't use it in apps that you submit to the Windows Store. ---------------------------------------------------------------------- Fibonacci · 2025-05-27 16:12 Who cares. I dont put my apps in the Windows Store, do you? ---------------------------------------------------------------------- gues1 · 2025-05-27 16:45 Quote from: paule32 on March 08, 2025, 07:30:06 pmI am a little bit the Fan of minimal Executables. But FPC is very fast for creating them. *.a Archive Files provide you a import.a file which contains the Procedures and Functions with a import Link to the DLL. Windows itself use DLL's to save Memory. And for me, it makes no sense, to store 100 MB Executables on the Computer disk. It makes me more Sense to save Memory and Disk Space by using DLL files Delphi as this option using the runtime BPLs (that are DDLs), and you can link to them so you exe is very minimal size (but you must distribute all BPLs that are runtime linked). But, once upon a time .... is not the begin of a novel, but a past nightmare. Who doesn't know about DLLs HELL ? One time the applications are linked to a lot of DLLs, not only system DLLs but also thirdy part DLLs. And this DLL stay in a common path, so the distribution of the applications is very compact. This also occurs 'cause internet speed that was low at that time, but not only for this. When the DLLs started to change there were an infinite problems (remember the moltitude of msvcrt.dll ?). Lazarus and Delphi normally create a monolithic EXE (I speak about Windows apps), without the use of external DLLs (of course excepts the Windows systems DLLs). I love this, expecially after lived a "DLLs HELL" time. And I think that more people love this. I used BPLs some time ago, but the patch released (that not always patched the BPLs) made me give up their use. And, when FPC or LCL will be updated, what about the old DLL the linked functions of LCL (now updated) ? You must recompile ALL your DLLs and verifiy the all functions, procedures, methods, etc ... use the same "interfaces". Should be better that like in Delphi there were standard DLLs available from LAZARUS and FPC and the possibility to link them at compile time, and of course the possibility to recompile them every time is needed (like IDE of Lazarus). But this is really a big big works. ---------------------------------------------------------------------- gues1 · 2025-05-27 16:52 Quote from: Fibonacci on May 27, 2025, 06:12:37 pmWho cares. I dont put my apps in the Windows Store, do you? Never put anything in Windows store, but ... normally I see that those advertisement mean that you applications will not be "accepted" very well from security apps. I can sign my exes, so may be that overload the security check but not all can sign their apps. Anyway thanks for looking and finding this feature. ---------------------------------------------------------------------- Fibonacci · 2025-05-31 18:14 Another compression API. DEFLATE via PresentationNative_v0300.dll, available since Windows 7. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program deflate; type  z_streamp = ^z_stream;  z_stream = record    next_in   : PByte;   // next input byte    avail_in  : DWord;   // number of bytes available at next_in    total_in  : DWord;   // total nb of input bytes read so far    next_out  : PByte;   // next output byte should be put there    avail_out : DWord;   // remaining free space at next_out    total_out : DWord;   // total nb of bytes output so far  msg       : PByte;   // last error message, NULL if no error    state   : Pointer; // not visible by applications    zalloc    : Pointer; // used to allocate the internal state    zfree     : Pointer; // used to free the internal state    opaque    : Pointer; // private data object passed to zalloc and zfree    data_type : Integer; // best guess about the data type: binary or text    adler     : DWord;   // adler32 value of the uncompressed data    reserved  : DWord;   // reserved for future use  end; const  Z_FINISH     = 4;  Z_STREAM_END = 1;  Z_BUF_ERROR  = -5; function ums_deflate_init(stream: z_streamp; level: integer; version: pchar; stream_size: integer): integer; stdcall; external 'PresentationNative_v0300.dll';function ums_deflate(stream: z_streamp; flush: integer): integer; stdcall; external 'PresentationNative_v0300.dll';function ums_inflate_init(stream: z_streamp; version: pchar; stream_size: integer): integer; stdcall; external 'PresentationNative_v0300.dll';function ums_inflate(stream: z_streamp; flush: integer): integer; stdcall; external 'PresentationNative_v0300.dll'; function _lopen(lpPathName: PChar; iReadWrite: Integer): THandle; stdcall; external 'kernel32.dll';function _lread(hFile: THandle; lpBuffer: Pointer; uBytes: Cardinal): Cardinal; stdcall; external 'kernel32.dll';function _lclose(hFile: THandle): Integer; stdcall; external 'kernel32.dll';function GetFileSize(hFile: THandle; lpFileSizeHigh: PCardinal): Cardinal; stdcall; external 'kernel32.dll'; function deflate(s: string): string;var  z: z_stream;  e: integer;begin  // alloc at least 64 bytes for the output  if length(s) > 64 then setlength(result, length(s)) else setlength(result, 64);   z.zalloc := nil;  z.zfree := nil;  z.opaque := nil;  z.avail_in := length(s);  z.next_in := @s[1];  z.avail_out := length(result);  z.next_out := @result[1];   if ums_deflate_init(@z, 9, '1', sizeof(z)) <> 0 then exit('');   e := ums_deflate(@z, Z_FINISH);  if e <> Z_STREAM_END then exit('');   setlength(result, z.total_out);end; function inflate(s: string): string;var  z: z_stream;  e: integer;begin  setlength(result, length(s)*2);   while true do begin    z.zalloc := nil;    z.zfree := nil;    z.opaque := nil;    z.avail_in := length(s);    z.next_in := @s[1];    z.avail_out := length(result);    z.next_out := @result[1];     if ums_inflate_init(@z, '1', sizeof(z)) <> 0 then exit('');     e := ums_inflate(@z, Z_FINISH);    if e = Z_BUF_ERROR then begin      // double the buffer      setlength(result, length(result)*2);      continue;    end;    if e <> Z_STREAM_END then exit('');    break;  end;   setlength(result, z.total_out);end; var  h: thandle;  original, compressed, decompressed: string; begin  // test file  h := _lopen('C:\Windows\explorer.exe', 0);  setlength(original, GetFileSize(h, nil));  _lread(h, @original[1], length(original));  _lclose(h);  writeln('original = ':30, length(original));   // compress  compressed := deflate(original);  writeln('compressed = ':30, length(compressed));   // decompress  decompressed := inflate(compressed);  writeln('decompressed = ':30, length(decompressed));   writeln('decompressed = original? ':30, decompressed=original);  writeln('compression ratio = ':30, trunc((1-(length(compressed)/length(original)))*100), '%');   readln;end. ---------------------------------------------------------------------- jamie · 2025-06-01 14:29 Let me get this straight, the current TZipper and TUnzipper does not generate correct ZIP files? I caught this thread because I just got done using the zStream unit to compress data in memory or optionally out to file as part of a larger code base.  As far as I know from looking at the sources it has no external LIB requirement. Jamie ---------------------------------------------------------------------- Fibonacci · 2025-06-01 14:53 Deflate – yes, TZipper/ZLib is just fine. Using undocumented APIs is a form of art. It allows you to create binaries that are an order of magnitude smaller than what you would get with TZipper. Why would anyone need such tiny binaries? Well, your imagination is the only limit. Shellcodes, packers, crypters, demoscene... As for D3D compression API - it outperforms ZIP by far.