Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

shell - how to unzip a zip file inside another zip file?

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.

question from:https://stackoverflow.com/questions/65889191/how-to-unzip-a-zip-file-inside-another-zip-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No idea about Jenkins but what you need is a recursive function.

recursiveUnzip.sh

#!/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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...