Last Updated on 14 October 2019 by Roberto De Pedrini
Have you ever had to generate a zip file and then send it via mail / ftp / … maybe starting from a folder of the IFS populated with files?
Here is a method to do this using the PASE shell embedded in a CLLE program.
List files contained in a folder
First of all it is better to create a workbook inside the / tmp (present by default on all releases) or inside one’s home (ex. / Home / myUsrPrf).
CRTDIR DIR ('/ home / PETRIS / zipTemp')
In order to get the list of files contained in an IFS folder it is possible to launch the ls command
CHGVAR VAR (& QSHCMD) VALUE ('cd myFolderIFS; ls *. *> /Home/PETRIS/zipTemp/listaFilesDaZippare.txt') QSH CMD (& QSHCMD)
Creating and populating zip files
Once the list of files is obtained, in order to manage a reading cycle we copy the text into a physical file
// Creating source files CRTSRCPF FILE (QTEMP / SRC) RCDLEN (212) MBR (* FILE) // Copy IFS txt file to source file CPYFRMSTMF FROMSTMF ('/ home / PETRIS / zipTemp / elencoFilesDaZippare.txt') TOMBR ('/ QSYS.LIB / QTEMP.LIB / SRC.FILE / SRC.MBR') MBROPT (* REPLACE) ENDLINFMT (* LF) // Copy from source file to physical file (previously created) CPYF FROMFILE (QTEMP / SRC) TOFILE (QTEMP / MYFLATFILE) MBROPT (* REPLACE) FMTOPT (* CVTSRC) ERRLVL (* NOMAX)
At this point we proceed to create the zip file (only the first time) and insert each of the files in the list
CHGVAR VAR (& NVOLTE) VALUE (1) CYCLE: RCVF MONMSG MSGID (CPF0000) EXEC (GOTO CMDLBL (FINECICLO)) IF COND (& NVOLTE * EQ 1) THEN (DO) / * & MYFLATFLD = field of the MYFLATFILE * / physical file CHGVAR VAR (& QSHCMD) VALUE ('cd myFolderIFS; ajar -cM /home/PETRIS/myZipFile.zip' * CAT & MYFLATFLD) QSH CMD (& QSHCMD) CHGVAR VAR (& NVOLTE) VALUE (0) ENDDO ELSE CMD (DO) CHGVAR VAR (& QSHCMD) VALUE ('cd myFolderIFS; ajar -aM /home/PETRIS/myZipFile.zip' * CAT & MYFLATFLD) QSH CMD (& QSHCMD) ENDDO
GOTO CMDLBL (CYCLE)END OF THE CYCLE:
Every time the operating system inserts a file into the zip it disappears from the IFS (in practice the file is moved from the folder to the archive)
At this point all that remains is to delete the files and the temporary folder
DLTF FILE (QTEMP / SRC) DLTF FILE (QTEMP / MYFLATFILE) DEL OBJLNK ('/ home / PETRIS / zipTemp / listaFilesDaZippare.txt') RMVDIR DIR ('/ home / PETRIS / zipTemp')
To carry out the opposite operation it is sufficient to use the same command with different parameter
cd /home / PETRIS / zipTemp; ajar -x /home/PETRIS/myZipFile.zip
at this point the system will have extracted the contents of myZipFile.zip in the zipTemp folder