Extract file from a zip inside another zip using 7z command line
Solution 1:
extract file from a zip inside another zip using 7z command line
Below is a 7Zip CLI scripted way I did this from a few years back. I've had to use and adjust it a time or two since to accommodate as-needed.
This extracts all zip files (and their contents) within other zip files (and their contents) until no other zip files remain to extract from and essentially traverses recursively from the starting [root] parent zip file until the last child zip file and its contents; extracting files from all zip and sub-zip files.
I've used this method to traverse through about four levels for some ridiculous reason where a company was sending data like this as a standard and could not change it but I was still able to automate the part I was given to do so.
CLI 7za Batch Script
Set the source, destination, and working directory variables for your need and copy the file into the source directory folder and then kick it off. Otherwise, you can change the *.zip
with <ParentZipFileName>.zip
.
:: This script uses the 7zip CLI to extract ZIP file(s) contents in one location to another
:: It then does an XCOPY of extracted ZIP files within the initial extacted files and copies those to a workdir
:: It then deletes ZIP files from source, and extracts the other ZIP files from workdir and loops until complete
:: NOTE that the 7za may need to have the environmental variable set accordinly
SET sourcedir=C:\Source
SET destdir=C:\Dest
SET workdir=C:\Workdir
IF NOT EXIST "%sourcedir%" MD "%sourcedir%"
IF NOT EXIST "destdir%" MD "%destdir%"
IF NOT EXIST "%workdir%" MD "%workdir%"
:unzip
7za -Y e "%sourcedir%" -o"%destdir%" -r
IF EXIST "%workdir%\*.zip" DEL /Q /F "%workdir%\*.zip"
XCOPY /Y /F "%destdir%\*.zip" "%workdir%\"
IF EXIST "%destdir%\*.zip" DEL /Q /F "%destdir%\*.zip"
DIR "%workdir%\*.zip" /A-D
ERRORLEVEL 1 GOTO done
:unzip2
7za -Y e "%workdir%" -o"%destdir%" -r
IF EXIST "%workdir%\*.zip" DEL /Q /F "%workdir%\*.zip"
XCOPY /Y /F "%destdir%\*.zip" "%workdir%\"
IF EXIST "%destdir%\*.zip" DEL /Q /F "%destdir%\*.zip"
DIR "%workdir%\*.zip" /A-D
IF ERRORLEVEL 1 GOTO done
GOTO unzip2
:done
GOTO EOF
Further Resources
- XCOPY
- DEL
- ERRORLEVEL
- 7ZA COMMANDS