bash 如何递归解压缩嵌套的 ZIP 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34208857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:01:11  来源:igfitidea点击:

How do I recursively unzip nested ZIP files?

linuxbashshellzipunzip

提问by AdHominem

Given there is a secret file deep inside a nested ZIP file, i.e. a zip file inside a zip file inside a zip file, etc...

鉴于嵌套 ZIP 文件深处有一个秘密文件,即 zip 文件内的 zip 文件内的 zip 文件,等等......

The zip files are named 1.zip, 2.zip, 3.zip, etc...

这个zip文件命名1.zip2.zip3.zip,等...

We don't know how deep the zip files are nested, but it may be thousands.

我们不知道 zip 文件的嵌套深度,但可能有数千个。

What would be the easiest way to loop through all of them up until the last one to read the secret file?

循环遍历所有这些直到最后一个读取机密文件的最简单方法是什么?

My initial approach would have been to call unziprecursively, but my Bash skills are limited. What are your ideas to solve this?

我最初的方法是unzip递归调用,但我的 Bash 技能有限。你有什么想法来解决这个问题?

采纳答案by AdHominem

Thanks Cyrus! The master wizard Shawn J. Goff had the perfect script for this:

谢谢赛勒斯!大师级巫师 Shawn J. Goff 为此编写了完美的脚本:

while [ "`find . -type f -name '*.zip' | wc -l`" -gt 0 ]; do find -type f -name "*.zip" -exec unzip -- '{}' \; -exec rm -- '{}' \;; done

回答by silverdrop

Here's my 2 cents.

这是我的 2 美分。

#!/bin/bash

function extract(){
  unzip  -d ${1/.zip/} && eval  && cd ${1/.zip/}
  for zip in `find . -maxdepth 1 -iname *.zip`; do
    extract $zip 'rm '
  done
}

extract '1.zip'

回答by user930412

Checkout this java based utility nzipfor nested zips.

查看这个基于 Java 的实用程序nzip以获取嵌套zip 文件

Extracting and compressing nested zips can be done easily using following commands:

使用以下命令可以轻松地提取和压缩嵌套的 zip:

java -jar nzip.jar -c list -s readme.zip 

java -jar nzip.jar -c extract -s "C:\project\readme.zip" -t readme 

java -jar nzip.jar -c compress -s readme -t "C:\project\readme.zip" 

PS. I am the author and will be happy to fix any bugs quickly.

回答by Ronan Boiteau

Probably not the cleanest way, but that should do the trick:

可能不是最干净的方法,但这应该可以解决问题:

#!/bin/sh
IDX=1 # ID of your first zip file
while [ 42 ]
do
    unzip $IDX.zip # Extract
    if [[ $? != 0 ]]
    then
        break # Quit if unzip failed (no more files)
    fi
    if [ $IDX -ne 1 ]
    then
        rm $IDX.zip # Remove zip to leave your directory clean
    fi
    (( IDX ++ )) # Next file
done