Wednesday, November 21, 2018

Using command to zip and unzip files

zipping and unzipping data to the ifs

In the past I have written about zipping files in the IFS using Java jar and APIs, this week a colleague showed me a simpler way to do it using a couple of IBM i commands.

He explained that these commands had been introduced in IBM i 7.2, and can zip and unzip files in the IFS as well as files in, what I call, the native IBM i environment. With these commands the zipped/compressed files are called "archive files", therefore, IBM named them:

 

CPYTOARCF copy to archive file

The CPYTOARCHF command has three parameters:

  1. FROMFILE:  Name of the file that will be compressed in the archive file
  2. TOARCF:  Name of archive file
  3. SUBTREE:  Whether all the subfolders should be archived too

The first thought I had when I was shown this command was how many bytes of space could the compressing save?

Created a file of one million records, and then used command:

01  CPYTOARCF FROMFILE('/qsys.lib/qtemp.lib/bigfile.file')
02            TOARCF('/MyFolder/zipped.zip')

Line 1: The path name of my file QTEMP/BIGFILE has to be given in "directory format", therefore, as the IBM i native environment is all in the QSYS.LIB folder, the file is in the QTEMP library which needs to be entered as QTEMP.LIB, and my file BIGFILE has to be entered with the file extension as BIGFILE.FILE. Unlike the IFS the IBM i environment is not case sensitive.

Line 2: The location of the archived file, which needs to be in the IFS, which is case sensitive. Here the archive file is called zipped.zip which is in the IFS folder MyFolder.

I do not need the SUBTREE parameter in this case as I am only saving one file.

The archived file, zipped.zip, is created by the command.

When the command had finished I compared the size of the file BIGFILE in QTEMP to the size of the archive file:

BIGFILE 719,388,672 bytes
zipped.zip 39,075,409 bytes
Difference 680,313,263 bytes

That is an enormous difference with this file. I know that the difference will be different depending on the file I copy, but this does show how much space could be saved.

I can also use this command to compress a file in the IFS.

01  CPYTOIMPF FROMFILE(QTEMP/EXTRACT1) +
02              TOSTMF('MyFolder/extract1.csv') +
03              MBROPT(*REPLACE) +
04              FROMCCSID(37) +
05              STMFCCSID(*PCASCII) +
06              RCDDLM(*CRLF) 

07  CPYTOARCF FROMFILE('MyFolder/extract1.csv') 
08              TOARCF('MyFolder/zipfile.zip')

Lines 1 – 6: First I want to copy my data from the IBM i environment to the IFS. You might need to do something different with the CCSID, line 4, as that depends on your IBM i's default CCSID.

Lines 7 and 8: Then I can compress the file that was copied to the IFS.

I could easily attach this file to an email and send it.

It is not possible to add to an archive file, or give the names of multiple files in the FROMFILE parameter. Therefore, if I want to archive more than one file I would need to place them in the same IFS folder and then use the following:

01  CPYTOARCF FROMFILE('/MyFolder')
02            TOARCF('/MyFolder/zipfile.zip')

By only giving the name of the folder all of the files within it are compressed into the zip file.

But that only does the current folder. If I wanted to compress the contents of all the subfolders too I would need to use the SUBTREE parameter:

01  CPYTOARCF FROMFILE('/MyFolder')
02              TOARCF('/MyFolder/zipfile.zip')
03              SUBTREE(*ALL)

 

CPYFRMARCF copy from archive file

Having archived/zipped/compressed a file or files how do I unarchive them? The CPYFRMARCF command does that.

The command has three parameters:

  1. FROMARCF:  Name of the archive file
  2. TODIR:  The folder where the uncompressed file will be placed.
  3. RPLDTA:  Whether to replace the existing data

The format of the command is pretty much the same whatever you are unarchiving, IFS files and IBM i environment files.

01  CPYFRMARCF FROMARCF('/MyFolder/zipfile.zip')
02               TODIR('.')
03               RPLDTA(*YES)

Line 1: I need to give the archive file's name and folder it is in.

Line 2: I have found that if I use the dot/period/full stop ( . ) in the to directory parameter the archived file(s) are unarchived into the IFS folder or IBM i library it was archive from.

Line 3: If I want to replace the existing files in the folder or library I would use *YES in the replace data parameter.

Experimenting with the putting values in the TODIR parameter I have found if want to restore the IFS file(s) to another IFS folder I must enter the folder's name without a leading slash ( / ). If the folder does not exist it is created by the command.

01  CPYFRMARCF FROMARCF('/MyFolder/zipfile.zip')
02               TODIR('AnotherFolder')

If I give a folder name with a leading slash, then a subfolder is created in the folder that the file was archived from.

01  CPYFRMARCF FROMARCF('/MyFolder/zipfile.zip')
02               TODIR('/SubFolder')

In this example the archived file(s) are restored to the subfolder /MyFolder/SubFolder.

I wish that restoring to the IBM i environments was as easy. I need to have a copy of the physical data file I originally archived in the library I archived it from, with the same name of the original file. I have to put the dot in the TODIR the file is restored to the library and file it was archived from.

01  CRTDUPOBJ  OBJ(BIGFILE) FROMLIB(*LIBL) +
02               OBJTYPE(*FILE) +
03               TOLIB(QTEMP) +
04               CST(*NO) TRG(*NO) ACCCTL(*NONE)

05  CPYFRMARCF FROMARCF('/MyFolder/zipped.zip') +
06               TODIR('.') +
07               RPLDTA(*YES)

If original file is not present in the library then a source physical file is created. Alas, the source data field the source file is wrapped so the file contains less than one record of data, the remainder is wrapped to the next record.

I have tried using the TODIR, even putting the path that the original file was archived from.

01  CPYFRMARCF FROMARCF('/PGMSDH/zipped.zip')
02                TODIR('/qsys.lib/qtemp.lib/bigfile.file')
03                RPLDTA(*YES)

Every time I see the following in the job log:

File BIGFILE created in library QTEMP.
Object QSYS in database file BIGFILE not found.
0 objects were replaced and 0 objects were newly
  created during decompression.
Information passed to this operation was not valid.

When I look at the last message I have to admit I do not find it useful in diagnosing the issue.

Message ID . . . . . . :   CPFA0A2
Message type . . . . . :   Escape

Message . . . . :   Information passed to this operation was 
   not valid.
Cause . . . . . :   Possible causes:
    --The operation could not use the data passed to it.
    --A name may not be correct.
    --Directory was expected, but a file was specified.
    --File was expected, but a directory was specified.
    --The function requested is not supported by the file system.
Recovery  . . . :   Check the input data to determine the cause
  of the problem.  Correct the error, and retry the operation.

If you know how to overcome this problem I would be grateful if you would share the details with me. You can use the Contact Form on the right side of this page, or add a comment to this post.

 

Having spent a couple of days playing with these commands I can see how useful they are for IFS files. In their current form I know I will not be using these commands for files in the IBM i environment.

 

 

This article was written for IBM i 7.3, and should work for 7.2 too.

1 comment:

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.