如何将一个 zip 文件解压缩到另一个 zip 文件中? [英] how to unzip a zip file inside another zip file?

查看:114
本文介绍了如何将一个 zip 文件解压缩到另一个 zip 文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如上所示,在Workspace 中,我们有多个zip 文件,在每个zip 文件中,存在另一个zip 文件application/app.zip.我想将 app1app2app3app4 解压缩到新文件夹中.我想使用与父 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
                
    '''

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

Btw, I am running this command inside my Jenkins pipeline.

推荐答案

不知道 Jenkins 但你需要的是一个递归函数.

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"

然后像这样调用脚本

./recursiveUnzip.sh <directory>

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

In you case, probably like this

./recursiveUnzip.sh "$WORKSPACE"

这篇关于如何将一个 zip 文件解压缩到另一个 zip 文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆