Recent

Author Topic: [Solved] Zipper - How to zip files without path information in the zip file  (Read 5020 times)

JanRoza

  • Hero Member
  • *****
  • Posts: 709
    • http://www.silentwings.nl
I've been reading the fpc zipper (paszlib) documentation and searching this forum for information about zipping files without path information but haven't found the answer yet.
Is it possible to zip files without path structure in the resulting zip file?
And if so can someone provide a simple example?
Thanks!
« Last Edit: January 11, 2018, 05:58:24 pm by JanRoza »
OS: Windows 11 / Linux Mint 22.1
       Lazarus 4.0 RC FPC 3.2.2
       CodeTyphon 8.70 FPC 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Zipper - How to zip files without path information in the zip file
« Reply #1 on: January 11, 2018, 02:42:18 pm »
By making a relative path? CreateRelativePath() from LazFileUtils

This works for me. The demo zips all pas files from lazarus/lcl with relative path names. Adapt the "basedir" to point to your lazarus/lcl folder.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, zipper, FileUtil, LazFileUtils;
  7.  
  8. const
  9.   zipfilename = 'test.zip';
  10.   basedir = 'C:\lazarus-trunk_fpc304\lcl';
  11.  
  12. var
  13.   zip: TZipper;
  14.   L: TStrings;
  15.   i: Integer;
  16.   relativefn: String;
  17. begin
  18.   zip := TZipper.Create;
  19.   try
  20.     zip.Filename := zipfilename;
  21.     L := TStringList.Create;
  22.     try
  23.       FindAllFiles(L, basedir, '*.pas', true);
  24.       WriteLn(L.Count);
  25.       for i:=0 to L.Count-1 do begin
  26.         relativefn := CreateRelativePath(L[i], basedir);
  27.         zip.Entries.AddFileEntry(L[i], relativefn);
  28.       end;
  29.     finally
  30.       L.Free;
  31.     end;
  32.     zip.SaveToFile(zipfilename);
  33.   finally
  34.     zip.Free;
  35.   end;
  36. end.

JanRoza

  • Hero Member
  • *****
  • Posts: 709
    • http://www.silentwings.nl
[Solved] Zipper - How to zip files without path information in the zip file
« Reply #2 on: January 11, 2018, 05:57:58 pm »
Thanks WP,
I had read something about relativepath but didn't grasp it.
Your example made it clear to me.
OS: Windows 11 / Linux Mint 22.1
       Lazarus 4.0 RC FPC 3.2.2
       CodeTyphon 8.70 FPC 3.3.1

 

TinyPortal © 2005-2018