Zip / Unzip via QSH

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

Recent Posts

VsCode Extention: Bob Cozzi’s RPG IV to RPG Free Conversion

👉 Review: Bob Cozzi’s RPG IV to RPG Free Conversion – a useful VS Code extension for RPG modernization If…

4 months ago

IBM i & SQL Tips #010 – Locating Programs in the Call Stack with STACK_INFO

Hello everyone, I’d like to highlight another excellent contribution by Massimo Duca, part of his ongoing IBM i & SQL…

4 months ago

Trying out “Display File DDS Edit” for VS Code

Intrigued by some recent posts from Cristian Larsen on LinkedIn (New Release – Display File DDS Edit v 0.10.1), I…

5 months ago

Project Bob: the next-gen AI partner for IBM i and IBM Z application development

Hello everyone, Today I’d like to draw your attention to a major new announcement from IBM: Project Bob — a…

5 months ago

IBM i & SQL Tips #6: Calling REST APIs and Parsing JSON Responses with SQL

I want to share with you a particularly useful article by Massimo Duca in the IBM i & SQL Tips…

6 months ago

How Parameter Passing Works in IBM i Programs (RPG / Cobol)

Hello everyone, I’d like to highlight a very useful article by Marco Riva on Markonetools, where he clearly explains how…

6 months ago