I have multiple zip files inside a folder and another zip file exists within each of these zip folders. I would like to unzip the first and the second zip folders and create their own directories.
Here is the structure
Workspace
customer1.zip
application/app1.zip
customer2.zip
application/app2.zip
customer3.zip
application/app3.zip
customer4.zip
application/app4.zip
As shown above, inside the Workspace
, we have multiple zip files, and within each of these zip files, there exists another zip file application/app.zip
. I would like to unzip app1
, app2
, app3
, and app4
into new folders. I would like to use the same name as the parent zip folder to place each of the results. I tried the following answers but this unzips just the first folder.
sh '''
for zipfile in ${WORKSPACE}/*.zip; do
exdir="${zipfile%.zip}"
mkdir "$exdir"
unzip -d "$exdir" "$zipfile"
done
'''
Btw, I am running this command inside my Jenkins pipeline.
No idea about Jenkins
but what you need is a recursive function.
#!/bin/dash
recursiveUnzip () { # $1=directory
local path="$(realpath "$1")"
for file in "$path"/*; do
if [ -d "$file" ]; then
recursiveUnzip "$file"
elif [ -f "$file" -a "${file##*.}" = 'zip' ]; then
# unzip -d "${file%.zip}" "$file" # variation 1
unzip -d "${file%/*}" "$file" # variation 2
rm -f "$file" # comment this if you want to keep the zip files.
recursiveUnzip "${file%.zip}"
fi
done
}
recursiveUnzip "$1"
Then call the script like this
./recursiveUnzip.sh <directory>
In you case, probably like this
./recursiveUnzip.sh "$WORKSPACE"