How do I recusively unzip nested ZIP files?

你离开我真会死。 提交于 2019-12-07 11:17:43

问题


Okay, 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...

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

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

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 unzip recursively, but my bash skills are limited. What are your ideas to solve this?


回答1:


Here's my 2 cents.

#!/bin/bash

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

extract '1.zip'



回答2:


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

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



回答3:


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



回答4:


Checkout this java based utility nzip for nested zips.

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

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.


来源:https://stackoverflow.com/questions/34208857/how-do-i-recusively-unzip-nested-zip-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!