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')

unzip

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

Related Posts
DB2 for i SQL – String Manipulation – POSSTR-LOCATE-LOCATE_IN_STRING (EN)

Introduction Often, in our applications, we need to work with text strings, and DB2 SQL can come in very useful Read more

DB2 for i – FAQ & Howtos (EN)

DB2 Database and SQL ... maybe the most important things on IBM i platform: here's a collection of FAQs, tips Read more

IBM i 7.4 Announcement (En)

Comes directly with the Easter egg this IBM announcement for the news of the IBM i 7.4 version, iNext version Read more

Generated Always Columns (EN)

Introduction "Generated Always Column": are columns, table fields, filled by DB2 engine: something like columns with a default value but Read more

About author

Head of Application development & Systems integration I have always worked on "i". Programmer, analyst, project manager, computer systems analyst, ... lots of hats but a single preferred platform. I collaborate with the FAQ400 community because I firmly believe in teamworking and I hope for a continuous and constant growth of the ecosystem of which I feel part.

Leave a Reply

Your email address will not be published. Required fields are marked *