如何将一个 zip 文件解压缩到另一个 zip 文件中?

时间:2021-01-25 17:04:51

标签: shell unix zip unzip

我在一个文件夹中有多个 zip 文件,并且每个 zip 文件夹中都存在另一个 zip 文件。我想解压第一个和第二个 zip 文件夹并创建它们自己的目录。
这是结构

Workspace
    customer1.zip
      application/app1.zip
    customer2.zip
      application/app2.zip
    customer3.zip
      application/app3.zip
    customer4.zip
      application/app4.zip

如上所示,在 Workspace 中,我们有多个 zip 文件,在每个 zip 文件中,存在另一个 zip 文件 application/app.zip。我想将 app1app2app3app4 解压缩到新文件夹中。我想使用与父 zip 文件夹相同的名称来放置每个结果。我尝试了以下 answers 但这只是解压缩了第一个文件夹。

   sh '''
        for zipfile in ${WORKSPACE}/*.zip; do
            exdir="${zipfile%.zip}"
            mkdir "$exdir"
            unzip -d "$exdir" "$zipfile"
        done
                
    '''

顺便说一句,我正在我的 Jenkins 管道中运行这个命令。

1 个答案:

答案 0 :(得分:1)

不知道 stopping_rounds = 2 但你需要的是一个递归函数。

recursiveUnzip.sh

Jenkins

然后像这样调用脚本

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

在你的情况下,可能是这样的

./recursiveUnzip.sh <directory>