Recent

Author Topic: How to execute PKZIPC from an application  (Read 693 times)

Hozso

  • New Member
  • *
  • Posts: 12
How to execute PKZIPC from an application
« on: January 06, 2026, 10:41:29 am »
  Hi guys,

  I need to use PKZIP started from a Lazarus graphic application... I would like to compress some complete directories. Unfortunately the directory names contain special Hungarian characters, spaces and hyphens. Probably because of this, I can not use the PKZIP from the batch file...
  I made some tests, using the oldfashioned filenames, with those it works fine! This is the batch file that works:
 
 
Code: Pascal  [Select][+][-]
  1. chcp 65001
  2. echo %cd%
  3. echo off
  4. for %%a in (%cd%) do set "DirName=%%~na"
  5. set Dir_1="FIGYELMEZTETŐ JELZÉSEK"
  6. set Dir_2="KALIBRÁLÁS"
  7. set Dir_3="KEZELÉS"
  8. set Dir_4="MÉRT ÉRTKEK"
  9. set Dir_5="MÉRTÉKEGYSÉGEK"
  10. set Dir_6="PLC JELEK"
  11. start "c:\Program Files\PKWARE\PKZIPC\" pkzipc -add -dir=current %DirName%_1.zip %cd%\%Dir_1% %cd%\%Dir_2% %cd%\%Dir_3%
  12. start "c:\Program Files\PKWARE\PKZIPC\" pkzipc -add -dir=current %DirName%_2.zip %cd%\%Dir_4% %cd%\%Dir_5% %cd%\%Dir_6%
  13. pause
  14.  

  It works fine at the directory system: "f:\Test\DirLevel2". But unfortunately it does not work at the directory system like "f:\Test\2023_10_02 Forráskút-13". There is something with the code tables probably... I tried to set the 852 codetable and the 65001, too. By the 65001 works partly: if the above directory names are old style and only the directory name at the actual directory are mixed with special characters.

  So, I decided to use Lazarus to call the PKZIP, but it is kind of worse...

  I use TProcess as follows:

Code: Pascal  [Select][+][-]
  1. ...
  2. AProcess:=TProcess.Create(nil);
  3. AProcess.Executable:='c:\Program Files\PKWARE\PKZIPC\PKZIPC.exe';
  4. AProcess.Parameters.Add(memParams.Text);
  5. AProcess.Options:=AProcess.Options+[poWaitOnExit]+[poUsePipes]+[poStdErrToOutPut];
  6. AProcess.Execute;
  7. memResults.Text:=PipeToStr(AProcess.Output);  //Here I get the returned messages from PKZIP
  8. AProcess.Free;
  9. ...
  10.  

  When I set the parameters field to the same as the batch file, I only get an error message after running the PKZIP.
  '- add -dir=current 2023_10_02.zip F:\Test\DirLevel2\Dir_test_1\*.*'

  The error message is:

  'PKZIP: (E12) Cannot find file: -add -dir=current 2023_10_02_1.zip f:\Test\DirLevel2\Dir_test_1\*.*  .zip'


   I tried some other variations, but always got the same error message as result...
« Last Edit: January 06, 2026, 12:01:25 pm by Hozso »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: How to execute PKZIPC from an application
« Reply #1 on: January 06, 2026, 11:18:52 am »
Why? The old pkzip format is supported by the standard FPC libraries?
./packages/zlib
Usually it is deflate format.
You should not have the need to execute pkzip unless you are compiling for 16 bit DOS and zlib is not supported for that.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Hozso

  • New Member
  • *
  • Posts: 12
Re: How to execute PKZIPC from an application
« Reply #2 on: January 06, 2026, 11:45:35 am »
Because it seemed to be easy... if it would have worked, I would have been done by now...

So, the question is still on!

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: How to execute PKZIPC from an application
« Reply #3 on: January 06, 2026, 02:57:42 pm »
Rather than depending on an external program I'd always try to find out whether the requested functionality is available in built-in units. Here, you'll probably be happy with the zipper unit coming with fpc (no extra installation needed) - see https://wiki.lazarus.freepascal.org/paszlib for examples how to apply it. Ask again if you have special questions with it.

Thausand

  • Sr. Member
  • ****
  • Posts: 458
Re: How to execute PKZIPC from an application
« Reply #4 on: January 06, 2026, 04:49:18 pm »
I have agree for Thaddy and wp and have use unit zipper.

But you example have error.

Code: Pascal  [Select][+][-]
  1. AProcess.Parameters.Add(memParams.Text);
  2.  

memParams have suggest is multiple parameter then code only add one parameter. This wrong and process output tell:
Quote
  'PKZIP: (E12) Cannot find file: -add -dir=current 2023_10_02_1.zip f:\Test\DirLevel2\Dir_test_1\*.*  .zip'
PKZip have try find file literal "-add -dir=current 2023_10_02_1.zip f:\Test\DirLevel2\Dir_test_1\*.*  .zip'" and no exist.

Code have need for add parameter individual.
Code: Pascal  [Select][+][-]
  1. AProcess.Parameters.Add('-add');
  2. AProcess.Parameters.Add('-dir="current");
  3. etc.
  4.  
I not know arguments pkzip so have try self.

Then you batch and error tell have use start. Start program have differ rule for how pass parameter. Please no have use start and have use pkzip for execute. If want execute script batch then use have use cmd (TProcess is not terminal emulator).
« Last Edit: January 06, 2026, 05:06:06 pm by Thausand »

Hozso

  • New Member
  • *
  • Posts: 12
Re: How to execute PKZIPC from an application
« Reply #5 on: January 07, 2026, 02:08:22 pm »
Thanks for the replies! Thausand: thanks for the tip! I tried to use the method you mentioned, I add the parameters separately. Now it seems like the PKZPC.exe is started, a command window pops up, but there is no indication about the process proceedings... When I start the compression from a batch file, I can see the progress of the compression, but now I can only see the cursor blinking at the top left corner... And it never finishes... I have to break the running... so the created .zip file is corrupt...

If I still can't get it work, I will check paszlib, but actually it takes some extra work and effort for me... though, if this TProcess call would work, I would be done... :-/

Thausand

  • Sr. Member
  • ****
  • Posts: 458
Re: How to execute PKZIPC from an application
« Reply #6 on: January 07, 2026, 10:19:07 pm »
@Hozso

When want do minimal work then no use tprocess. RunCommand or RunCommandInDir is need not all that many code that TProcess have need. But runcommand have drawback (have read below)

fwiw: I not have windows and not have pkzip (I no want use pkzip for Linux) then I not can test for you.

Is good that now is execute pkzip. Is progress  :)

Why pkzip not continue I not know but can have guess that is error somewhere in argument and pkzip is confuse or wait for something is happen (we not know what, and this problem)

There is many thing happen and need for work pkzip TProcess commandline and do what you have want. This is list of things that have know:
- If use TProcess or RunCommand then output is only catch when command finish when use option poWaitOnExit.
- pkzip is unknown variable (undocument) in how is behave (you can know if familiar pkzip). Then better is for make test for command that is know (dir, tree, etc) and make test simple then can test all configure option for TProcess/RunCommand or,
- when pkzip have option for example have simple show version number then can use for test. Then make pkzip have show list of files in archive, then make small complicate for example make single file compress to zipfile, etc.
- TProcess/RunCommand have statuscode/exitcode etc so when command is continue then can read value and value is tell what exact wrong. Description that pkzip is 'not contue' with window and cursor is no precise and not help solve problem. Ofc, then have know it 'hang' but not is tell why.

If execute program have many output and want show output (realtime) then is need more difficult implement for TProcess. Wiki have example:
https://wiki.lazarus.freepascal.org/Executing_External_Programs#Reading_large_output

If want now where program is 'hang' then have use message print or use step-debug and see where is 'hang'.

There is more quick/test solution: use runcommand(indir) have run cmd.exe and cmd.exe is load/execute batch. Have notice for cmd.exec that have other rule for add parameter (have read options for cmd.exe how is set parameter for execute batch). Pascal TStringlist can make Batchscript and save then have execute process cmd.exe for run this batch-script.

Now maybe have better understand why is advise for use tzipper because t(un)zipper many more easy, no have depends, have wiki-example and have more example in forum :-)

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: How to execute PKZIPC from an application
« Reply #7 on: January 07, 2026, 11:25:22 pm »
In order to motivate you to check out zipper I wrote the attached simple demo, zipdi,r which can pack all files following a given mask from an unlimited number of directories into a single zip file:

Code: [Select]
zipdir test.zip *.pas;*.lfm c:\MyProjects\Project1 c:\MyProjects\Project2
The zipping code itself requires only 5 lines.

Hozso

  • New Member
  • *
  • Posts: 12
Re: How to execute PKZIPC from an application
« Reply #8 on: January 12, 2026, 08:17:36 am »
Thanks for your help, guys! Fortunately I could manage the original idea. I realized that I had some problem with using pipes, but I do not need to use this function. And actually I just have to write a tiny little program to compress a bunch of directories, I will use it only for a few times a year. :D If I will have to do simething more sophisticated in the future, I will sure use paszlib!

 

TinyPortal © 2005-2018