HowTo: Add Directory Zipping and Unzip file type checking to the Zip Menu Utility
Novell Cool Solutions: Feature
Digg This -
Posted: 29 Oct 2004
|Learning to use Linux at Home and Work
Welcome to my ongoing series of HowTo articles designed to help Linux newbies get comfortable with Linux. Before trying any of these HowTos, take a few minutes to study the prerequisites so you can hit the ground running.
This HowTo explains a simple way of adding directory zipping functions to the Zip Menu utility and also zip type checking for the Unzip button. The additions explore if then else logic constructs and introduce the RunRev "contains" string comparator. See last week's article for background.
This is the Zip Menu window. Nothing visual will be changed, but the scripts for the card, the files field and all three buttons will be modified.
When we click on a directory, instead of going there the script will use the answer command to see if you want to zip it or not. If you do, it sets a flag and puts the name into the Files to be zipped field. The Zip button scripts need to know the file is a directory so that they can call a directory zipping shell script. The flag is passed using the global operator. This is put in the open card script and the flag is given a non directory status, so that it only works when we want it to.
These additions are all about simple logic statements, which mostly consists of getting the if then else statements in the right order.
The next picture shows what happens when you click on a directory
Maybe a better message would be “Zip this Directory?” These are the script modifications needed to make this Answer pop up work.
This is the card script, It shows the addition of the lines that set the global attribute for the variable ADIR and sets this to “F”. ie anything that is not the “D” we use later. The rest is as it was before.
Next we will look at the files field script where all the hard work goes on. Luckily the computer does it for us. First though we tell it about our global ADIR
You will notice that the new logic starts from the line if ThisFile <> empty. This line is here because when we click on the blank line to go back to the previous directory it would ask us if we wanted to zip it. Not what we want. Then it does the “Yes” answer stuff by setting ADIR to “D” and putting the directory name into the zipping file field.
We back out of this logic construct with two end ifs, then check ADIR to see if it is not set to “D” which means we said “No” and we can go to that directory and display its contents like before, else its an ordinary file.
Phew! Getting the hang of this is probably the hardest thing you ever have to learn with scripting.
Now we have to modify the zip button scripts.
This is the Zip Only button script. Once again we let it know about our global ADIR.
Then we insert the if ADIR = “D” logic. If it is a directory, we call a new shell script called dirzip.sh and reset ADIR to “F” else we call the old zip only shell script. The rest is the same except at the end I've included the line which put the GO Back message in the files field.
That was easy. Its almost the same for the zip + encrypt button.
In this script the called shell script is dirzippy.sh.
The last script to modify is the one for the unzip button. Here we are going to check that the file we choose to unzip ends with a .zip otherwise it does nothing.
The RunRev operator “contains” will be used in an if statement. If ZipFile contains “.zip” then do the unzip. Of course if a file has a funny name like myfile.zip.txt then the shell will try to run, but won't work, which is what used to happen with non .zip files. This just makes the script neater.
Where it says #Line moved to above, that is the line
put field “ZipFld” into ZipFile
This has to be before the if statement so we can do the contains comparison.
Notice the Go Back message is also included.
Now we have to create the two new shell scripts. These are much the same as the original ones, so you can copy these to the new names and the modify them. Copying an executable shell script to a new name gives the copy the same executable attribute, which is what we want.
Modify the copied shell scripts so that they look like these.
Notice that the Field Separator is set to “/” which gets rid of the trailing “/” and the filename and folder name are the same ie $FN
The -r recursively zips all subdirectories
Save all files. Test the application with the RunRev browse tool. When all is OK build the application using the same name as before, copy the executable to your bin folder, and voila, you now have a professional tool for zip/unzip files and directories.
You can zip up your Documents directory and burn it to a CD for safe keeping. This utility only deals with the zip and unzip shell tools. You could extend its use for other formats if you like and can. Or you can duplicate this one to work with the other formats by copying and modifying the shell scripts. I think this is a more “Linux” way. Each tool doing its own thing really well.
For more information about Runtime Revolution visit http://www.novell.com/coolsolutions/feature/1863.html
Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com