Recent

Author Topic: 7zip or zip component  (Read 3350 times)

guest48704

  • Guest
7zip or zip component
« on: July 23, 2019, 08:55:28 pm »
Is there a 7zip or zip component for lazarus?  Thanks

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: 7zip or zip component
« Reply #1 on: July 23, 2019, 09:10:14 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: 7zip or zip component
« Reply #2 on: July 23, 2019, 09:12:21 pm »
There are several, including fully native. packages zlib, unzip and libtar come even as standard. (I am not complete). Start with zlib.
Specialize a type, not a var.

guest48704

  • Guest
Re: 7zip or zip component
« Reply #3 on: July 23, 2019, 10:20:43 pm »
Thanks.

It isn't working correctly for me:

Code: [Select]
procedure TForm1.Test1Click(Sender: TObject);
var OurZipper: TZipper; I: Integer;
begin
    OurZipper := TZipper.Create;
    try
      OurZipper.FileName := Application.Location + '\.UserKeys\archive.zip';

      for I := 2 to ParamCount do
          OurZipper.Entries.AddFileEntry(Application.Location + '\.UserKeys\john', Application.Location + '\.UserKeys\john2william');

      OurZipper.ZipAllFiles;
    finally
      OurZipper.Free;
    end;
end;   

It creates the archive.zip, but nothing is in it.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: 7zip or zip component
« Reply #4 on: July 23, 2019, 10:34:27 pm »
What are the parameters that you pass to the application? Note that the first parameter is not used ("for I := 2 to Paramcount...")
Your code works correctly for me when I specify filenames directly.

guest48704

  • Guest
Re: 7zip or zip component
« Reply #5 on: July 23, 2019, 11:04:02 pm »
 I was thinking that you could put a list of files in OurZipper.Entries.AddFileEntry.  This works ok:

Code: [Select]
procedure TForm1.Test21Click(Sender: TObject);
var OurZipper: TZipper; I: Integer;
begin
    OurZipper := TZipper.Create;
    try
      OurZipper.FileName := Application.Location + '\.UserKeys\archive.zip';

      OurZipper.Entries.AddFileEntry(Application.Location + '\.UserKeys\john');
      OurZipper.Entries.AddFileEntry(Application.Location + '\.UserKeys\john2ann');

      OurZipper.ZipAllFiles;
    finally
      OurZipper.Free;
    end;
end;

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: 7zip or zip component
« Reply #6 on: July 24, 2019, 12:06:09 am »
I was thinking that you could put a list of files in OurZipper.Entries.AddFileEntry. 
Yes. Why shouldn't this be possible?

The problem in your example probably was that you did not pass parameters to your exe. Assuming that your project is called "zipper" (like in the wiki example, although not clearly specified there!) and you want to add the files "john" and "john2william" to the zip file you call the program like this:

Code: [Select]
  zipper john john2william
This is valid for runtime. In your code the parameter "john" is available as ParamStr(1), the parameter "john2william" as ParamStr(2). If you want to use these parameters already in the IDE, go to "Run" > "Run parameters" and type "john johnwilliams2" (without quotes) into the box "Command line parameters". Then in the ButtonClick routine you add the path

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   OurZipper: TZipper; I: Integer;
  4. begin
  5.     OurZipper := TZipper.Create;
  6.     try
  7.       OurZipper.FileName := Application.Location + '\.UserKeys\archive.zip';
  8.       for i := 1 to ParamCount do
  9.         OurZipper.Entries.AddFileEntry(ParamStr(i));
  10.         // or add an extra path, or whatever you need:
  11.         // OurZipper.Entries.AddFileEntry(Application.Location + '\.UserKeys\' + ParamStr(i));
  12.       OurZipper.ZipAllFiles;
  13.     finally
  14.       OurZipper.Free;
  15.     end;
  16. end;

Note that the index i begins here at 1, not at 2 like in your example. The value 2 was valid for the example in the wiki which you copied without thinking. The wiki project seems to create a binary "zipper" which expects the 1st commandline parameters to be the name of the zip file and the following parameters to be the names of the files to be added to the zip, like in zipper newzip.zip autoexec.bat config.sys

Alternatively, you can pass a stringlist with file names to the method Entries.AddFileEntries, or shorter, to the method ZipFiles. You can populate the stringlist by calling the procedure FindAllFiles (in unit FileUtils). The following code packs all relevant source files of a project into archive.zip:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   List: TStringList;
  4.   zip: TZipper;
  5. begin
  6.   List := TStringList.Create;
  7.   try
  8.     // Read all relevant file names of the project
  9.     FindAllFiles(List, Application.Location, '*.pas;*.lfm;*.lpr;*.lpi', false);
  10.       // false --> don't search subdirectories
  11.  
  12.     zip := TZipper.Create;
  13.     try
  14.       zip.FileName := Application.Location + 'archive.zip';
  15.       zip.ZipFiles(List);
  16.       // or
  17.       {
  18.       zip.Entries.AddFileEntries(List);
  19.       zip.ZipAllFiles;
  20.       }
  21.     finally
  22.       zip.Free;
  23.     end;
  24.   finally
  25.     List.Free;
  26.   end;
  27. end;

 

TinyPortal © 2005-2018