Tip for Unzipping Files and Handling Exceptions

The following test case can be used for unzipping downloaded zip files.
Copy and paste the following to any new test case, (edit for your requirements).

import com.kms.katalon.core.util.internal.ZipUtil

//Assumes there is an example.zip file in the user's download folder

// Concatenate the user's home directory with "/Downloads" to create a string representing the downloads folder path.
String downloadsFolder = System.getProperty("user.home") + "/Downloads";

// Create a new file object representing a zip file in a specified folder.
// You could use a GlobalVariable here ...
def zipFileName = 'example.zip'
File zipFile = new File(downloadsFolder + "/" + zipFileName);

// Create a new file object with the path specified by combining the downloads folder and "/unzipped".
// You could use a GlobalVariable here ...
def destDirectory = 'unzipped'
File destDir = new File(downloadsFolder + "/" + destDirectory);

/*
 *  Unzip a file and handle any exceptions that may occur.
 *  1. Attempt to extract the zip file to the specified destination directory.
 *  2. If successful, print a success message.
 *  3. If an exception occurs, catch it and print an error message with the exception message.
*/

try {
	ZipUtil.extract(zipFile, destDir);
	println("File unzipped successfully");
} catch (Exception e) {
	println("Error unzipping the file: " + e.getMessage());
}

5 Likes

Thank you @Dave_Evers for sharing this tip with us.

Hi em @Elly_Tran, can we make sure to promote Dave’s topic on our social media as well? Thanks!

1 Like