How do I recursively unzip nested ZIP files?

14,880

Solution 1

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

Solution 2

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'
Share:
14,880
AdHominem
Author by

AdHominem

Coffee addicted software engineer in the field of IT security.

Updated on June 22, 2022

Comments

  • AdHominem
    AdHominem almost 2 years

    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?

  • Joe F
    Joe F over 8 years
    Great solution, except in the pathological case of zip quines like droste.zip
  • AdHominem
    AdHominem over 8 years
    Well I guess the zip quine is a special case, but not that relevant when the goal is to ultimately hide some data inside the zip queue.
  • vinays84
    vinays84 over 3 years
    This helped me with my nested zip case. Though I should note that I needed to (1) Modify unzip $1 -d ${1/.zip/} && eval $2 && cd ${1/.zip/} to be three separate lines, (2) Put *.zip in single quotes, (3) Add cd .. after the for loop. Also, this doesn't handle zips files that are in subdirectories of the parent zip.