Recent

Author Topic: paszlib. Compress directory tree into a buffer  (Read 724 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 353
paszlib. Compress directory tree into a buffer
« on: October 30, 2025, 09:08:39 pm »
Hello.

I have a directory and I want to compress it content into a buffer. Maybe someone have already code for that.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: paszlib. Compress directory tree into a buffer
« Reply #1 on: October 31, 2025, 01:09:49 am »
I have a directory and I want to compress it content into a buffer. Maybe someone have already code for that.
If want simple then wiki have example https://wiki.freepascal.org/paszlib#Zipping_a_whole_directory_tree

When wiki example then make change
Have Filename (https://www.freepascal.org/docs-html/fcl/zipper/tzipper.filename.html) set temp file (https://www.freepascal.org/docs-html/rtl/sysutils/gettempfilename.html). Have temp directory for temp filename.
Make set inmemsize (https://www.freepascal.org/docs-html/fcl/zipper/tzipper.inmemsize.html) for use memory. When deplete then is buffer write file.
When have ZipAllFiles then make stream and use SaveToStream (https://www.freepascal.org/docs-html/fcl/zipper/tzipper.savetostream.html) for have zip stream buffer. If then can save to file and test if is valid zip. Then have stream destroy (or use later code).
Make clean temp file.

LemonParty

  • Sr. Member
  • ****
  • Posts: 353
Re: paszlib. Compress directory tree into a buffer
« Reply #2 on: November 01, 2025, 01:50:31 pm »
Not understood everything, but thank you.

One more question. How to make paszlib compress not only files but also empty directories?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: paszlib. Compress directory tree into a buffer
« Reply #3 on: November 02, 2025, 03:38:40 am »
One more question. How to make paszlib compress not only files but also empty directories?
Paszlib example do when add name empty directory TheFileList. Problem FindAllFiles no find empty directory.

LemonParty

  • Sr. Member
  • ****
  • Posts: 353
Re: paszlib. Compress directory tree into a buffer
« Reply #4 on: November 02, 2025, 09:30:53 pm »
Here is my function for archiving the content of directory include empty directories:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., Zipper, FileUtil, StrUtils;
  3.  
  4. {if Dir is empty this function return true but archive will not be created}
  5. function Zip(Dir: String; ZipFileName: String): Boolean;
  6. var
  7.   AZipper: TZipper;
  8.   szPathEntry: String;
  9.   i: SizeInt;
  10.   ZEntries: TZipFileEntries;
  11.   List: TStringList;
  12. begin
  13.   AZipper := TZipper.Create;
  14.   Result:= False;
  15.   try
  16.     try
  17.       AZipper.Filename := ZipFileName;
  18.       AZipper.Clear;
  19.       ZEntries := TZipFileEntries.Create(TZipFileEntry);
  20.  
  21.       If DirectoryExists(Dir) then begin
  22.         List:= TStringList.Create;
  23.         try
  24.           FindAllDirectories(List, Dir); {this will also grab empty directories}
  25.           FindAllFiles(List, Dir);
  26.  
  27.           for i:= 0 to Pred(List.Count) do
  28.             // Make sure the Dir files are not in the root of the ZipFile
  29.             ZEntries.AddFileEntry(List[i], CreateRelativePath(List[i], Dir));
  30.         finally
  31.           List.Free;
  32.         end;
  33.       end;
  34.       if ZEntries.Count > 0 then
  35.         AZipper.ZipFiles(ZEntries);
  36.       except
  37.         On E: EZipError do
  38.           E.CreateFmt('Zipfile could not be created%sReason: %s', [LineEnding, E.Message])
  39.       end;
  40.     Result := True;
  41.   finally
  42.     ZEntries.Free;
  43.     AZipper.Free;
  44.   end;
  45. end;
Example of usage:
Code: Pascal  [Select][+][-]
  1. Zip('C:\MyDir\'{must include directory separator}, 'C:\Archive.zip');
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

LemonParty

  • Sr. Member
  • ****
  • Posts: 353
Re: paszlib. Compress directory tree into a buffer
« Reply #5 on: November 02, 2025, 09:48:41 pm »
I am interested if TZipper have a compression level to set?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: paszlib. Compress directory tree into a buffer
« Reply #6 on: November 03, 2025, 03:06:07 am »
I am interested if TZipper have a compression level to set?
Yes, TZipper have. (https://www.freepascal.org/docs-html/fcl/zipper/tzipfileentry.compressionlevel.html)

When you example code have AddFileEntry (https://www.freepascal.org/docs-html/fcl/zipper/tzipfileentries.addfileentry.html) then exist version AddFileEntry and have return TZipFileEntry (https://www.freepascal.org/docs-html/fcl/zipper/tzipfileentry.html). That can have set property compressionlevel.

Why no read documentation ? Then no have wait answer  ;)

LemonParty

  • Sr. Member
  • ****
  • Posts: 353
Re: paszlib. Compress directory tree into a buffer
« Reply #7 on: November 03, 2025, 04:32:29 pm »
Why no read documentation ? Then no have wait answer  ;)
I suspect compression level to be in TZipper, so didn't notice it. Thank you for help.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Sr. Member
  • ****
  • Posts: 389
Re: paszlib. Compress directory tree into a buffer
« Reply #8 on: November 03, 2025, 06:48:03 pm »
@LemonParty
When compressionlevel for entry then all item can have compressionlevel self and have level other. Some time you no want big file have compression high (example: video file and have compression self then no do compression again).

.. YW. Have make fun program !  :)

 

TinyPortal © 2005-2018