Skip to main content  
        iSeries home   |   Easy400     |   CGIDEV2     |   MMAIL  
Freeware
 
 
 
About it
Prerequisites
Installation
Examples
Programming
Execution environment
Simple commands
 Download
 
 

 
ZIP & UNZIP on IBM i
Run them on IBM i, manually or through programs
by Giovanni B. Perotti (Italy)

1-About it

This small utility installs commands ZIP and UNZIP on your IBM i.
Once installed, commands zip and unzip can be run:

  • in an i5/OS PASE interactive terminal session (started by command CALL QP2TERM)
  • in a QSHELL interactive session (started by command STRQSH)
  • from a program issuing command
    STRQSH CMD(command_to_be_executed)

Note 1 - The original information about installing these commands can be found in the IBM IBM i Redbook IBM HTTP Server (powered by Apache), APPENDIX C: Bringing Zip and Unzip to i5/OS PASE and QShell Environments by Brian Smith.
This utility, however, makes the installation much easier.

Note 2 - Check out the updates to this tool by pressing this link.
To know the release date of this tool on your IBMi, enter command zip/released.

2-Prerequisites

  • OS/400 release V5R2 or subsequent

3-Installation

  • Download file zip.zip from the Easy400 download page and unzip it.
  • Follow the Readme.txt instructions to upload and to restore library ZIP.
  • On the iSeries run the following procedure:
    STRREXPRC SRCMBR(INSTALL) SRCFILE(ZIP/QREXSRC)
    It does the following:
    • Restores directory /home/zip
    • copies commands ZIP and UNZIP to directory /QOpenSys/usr/bin for use of i5/OS PASE
    • copies commands ZIP and UNZIP to directory /usr/bin for use of QSHELL

4-Examples of commands

  1. Command ZIP
    • zip /zip/x.zip /test/a.txt /test/b.txt
      This command compresses files /test/a.txt and /test/b.txt into file /zip/x.zip.
      The compressed files maintain their directory paths. Directory paths are recreated when these files are inflated on a PC.
    • zip -j /zip/x.zip /test/a.txt /test/b.txt
      This command is similar to the previous one. However, in this case the compressed files do not maintain their directory paths and directory paths are not recreated when these files are inflated on a PC.
    • zip -r /zip/x.zip /test
      This command compresses all the files in directory /test into file /zip/x.zip.
      The compressed files maintain their directory paths.
    • zip -jr /zip/x.zip /test
      This command compresses all the files in directory /test into file /zip/x.zip.
      The compressed files do not maintain their directory paths.
  2. Command UNZIP
    • unzip /zip/x.zip
      this command extracts files /test/a.txt and /test/b.txt,
      but would prompt if a file already exists.
      Notes:
      1. The target directory for the extracted files is the current directory, usually /home/user_profile_name
      2. One may change the current directory (BEFORE STRQSH) with command chgcurdir.
      3. The original file directories are maintained.
    • unzip -o /zip/x.zip
      this command extracts files /test/a.txt and /test/b.txt
      Note:
      1. Modifier -o allows to overwrite existing files without prompting for an user decision
    • unzip -n /zip/x.zip
      this command extracts files /test/a.txt and /test/b.txt
      Note:
      1. Modifier -n does not overwrite existing files and does not prompt for an user decision
    • unzip -j /zip/x.zip
      this command extracts files a.txt and b.txt
      Note:
      1. Modifier -j ignores the directories of the original files
    • unzip /zip/x.zip -d /tmp
      this command extracts files /tmp/test/a.txt and /tmp/test/b.txt.
      Note:
      1. Option -d target_directory allows to specify a target directory
    • unzip -P bigblock /test/crypted.zip -d /tmp
      this command unzips crypted file /test/crypted.zip by providing the password "bigblock".

    • My favorite UNZIP format:
      unzip -oj /zip/x.zip -d /tmp

5-Running commands from a program

  1. CLLE program
                  PGM
     /*Perform the unzip of file /zip/x.zip                       */
     /*           - /zip/x.zip                                    */
     /* containing the deflated files:                                 */
     /*           - /test/a.txt                                        */
     /*           - /test/b.txt                                        */
     /*The following files will be inflated:                           */
     /*           - /tmp/a.txt                                         */
     /*           - /tmp/b.txt                                         */
                  STRQSH     CMD('unzip -oj /zip/x.zip -d /tmp')
     /*Back to caller                                                  */
                  RETURN
                  ENDPGM
  2. RPGLE program
    D DoCmd           pr            10i 0                                
    D  cmd                        2000    const varying options(*varsize)
    D rc              s             10i 0                                
     *===================================                                
                                                                         
     /free                                                                     
       //Perform the unzip of file /zip/x.zip                     
       //           - /zip/x.zip                                  
       // containing the deflated files:                            
       //           - /test/a.txt                                  
       //           - /test/b.txt                                  
       //The following files will be inflated:                     
       //           - /tmp/a.txt                                   
       //           - /tmp/b.txt                                   
         rc=docmd('STRQSH CMD(''unzip -oj /zip/x.zip -d /tmp'')');
                                                                         
        //back to caller                                                 
          return;                                                        
     /end-free
                                                                         
     *===================================                                
     * docmd local subprocedure                                          
     * Executes a CL command                                             
     * Returns 0 if executed without error; otherwise 1.                 
    Pdocmd            b                                                  
    D docmd           pi            10i 0                                
    D   cmd                       2000    const varying options(*varsize)
                                                                         
    D qcmdexc         pr                  extpgm('QCMDEXC')              
    D  theCmd                     2000    const                          
    D  theCmdLen                    15p 5 const                          
                                                                         
     /free                                                               
       monitor;                                                          
         qcmdexc(%subst(cmd:1:%len(cmd)):%len(cmd));                     
       on-error;                                                         
         return 1;                                                       
       endmon;                                                           
       return 0;       
     /end-free         
                       
    Pdocmd            e

6-Execution environment

When a ZIP or UNZIP command is executed from a program running in interactive mode, the actions taken are displayed in the QSHELL terminal session:
Figure 1- Interactive mode

When a ZIP or UNZIP command is executed from a program running batch mode, the actions taken are printed in a job spool file named QPRINT:
Figure 2- Batch mode

If a program must run an UNZIP command and find out the names of the inflated files, it should operate as follow:

  1. submit a batch job that runs command
    STRQSH CMD('unzip ...')
  2. convert the spool file QPRINT from that job (see Figure 2) to a database file
  3. read this database file to retrieve the names of the inflated files.

7-Simple commands

In order to make developers life easier, two simple standard commands are available: ZIP/ZIP and ZIP/UNZIP.
These commands submit a batch job which runs the appropriate
STRQSH CMD('...')
command.
The result from a command can be displayed by specifying parameter DSP(*YES).

Figure 3- Command ZIP/ZIP
Stream files / directories (STMFS) - You can specify up to 50 names of IFS objects to be compressed. Each entry can be
  • a stream file name, or
  • a directory name. When a directory name is specified, all the objects in that directory are taken.
Note: Generic names - ending with an asterisk (*) - are not supported.
If you specify *YES for parameter JUNK ("Don't record directory names"), the stream files are zipped without their directory paths.

Figure 4- Command ZIP/UNZIP