Tutorial: Uncompressing a .zip file and accessing the contained files

GitHub: Robin Examples - Uncompressing a .zip file and accessing the contained files

In this tutorial we are going to utilize the .zip file we created during the Tutorial: Creating and compressing a simple text file.

As always we are going to use practices that make our scripts easier to read, more dynamic and maintainable.

We are going to utilize the Folder module a lot in this tutorial.

We begin with creating our working folder by using the Folder.Create action.

Hit Run in order to create the requisite folder.
You can see that the folder has been created.
The ‘WorkingFolder’ variable holds the output of the action, which is the path to our working folder.

Now we want get the files we generated in our previous tutorial.
We utilize the Folder.GetFiles action.

Take a look at the ‘FileFilter’ input.
We know that we already want a .zip file.
By using the '.zip’ as a filter we are telling the action to return us all () the zip files it finds inside the specified folder.
If we left the ‘FileFilter’ input as it was generated we would retrieve all the files contained in the folder.
You can also see a series of other inputs, used for sorting the returned results.

Note: In Robin you can delete default action inputs if you don’t want to change their value.
Even if you delete them in order to ‘de-clutter’ your script, they will retain their default value.

The ‘GetFiles’ action returns a list that holds the filepaths to all the retrieved files from a specified folder.
We know there is only one file in the folder so we are going to get the first entry in the list by
setting ‘ZipFile’ variable to ‘Files[0]’.
Remember, counting in Robin starts from 0 so in order to get the first (and only in this case) entry from the list we refer to the 0 index position.

We can then use the File.Copy action to copy the retrieved .zip file into our working folder.

Now we need to retrieve our newly copied .zip file, this time from our working folder.

Let’s up things a bit.
We will implement a simple mechanism that checks whether the .zip file exists or not.
If it does it will display a notification dialog to the user, informing him that the file was found and that it will be uncompressed.
If the file is not present for any reason, an error dialog will be displayed with the proper message.
We will use a simple if-else conditional mechanism.

We begin by creating a boolean variable that will get its value from the File.Exists action.
This action checks whether a file exists or not and returns a true or false boolean result.

Then we check that variable…

If it is true the following dialog will be displayed:
By hitting ‘OK’, the script proceeds with the uncompression.

You can check that the file is now uncompressed yourself, by opening the working folder:

We also implemented an ‘else’ (fallback) approach, in case the file does not exist:

Note: You can check the “Error” display by modifying the DoesFileExist variable.
By giving it a False value before the if-then-else condition starts…

it will move to the ‘else’ part.

An appropriate error dialog will pop-up:

Now that our file is finally uncompressed we can access and modify it.
First we retrieve it:

We can create a variable to store the new filepath.
We can utilize the ‘name’ property of the text file we retrieved from the ‘File[]’ list.
This way our script is becomes more agnostic and we don’t need to hard-code paths.

Now let’s read the file’s contents, and display them to the user:

Let’s create a new .txt file and copy the contents we have retrieved from the uncompressed .txt there.

Repetition is a great way to strengthen our skills so let’s display the contents we retrieved to the user by utilizing the Display.ShowMessage action once again…

Finally let’s retrieve all the .txt files from the working folder and display them to the user.

There should be two .txts in there…

Be sure to check the following topics from the docs:

The complete script for reference:

Best regards,

James Papadimitriou
Technical Evangelist

1 Like

Added link to the GitHub repo where the source file can be found.

Best regards,

James Papadimitriou
Technical Evangelist