Most elegant way to extract a directory from a zipfile using PowerShell?

powershell

Solution

As far as the folder location in a zip is known, the original code can be simplified:

$source = 'c:\tmp\test.zip'  # zip file
$target = 'c:\tmp\output'    # target root
$folder = 'test\etc\script'  # path in the zip

$shell = New-Object -ComObject Shell.Application

# find script folder e.g. c:\tmp\test.zip\test\etc\script
$item = $shell.NameSpace("$source\$folder")

# actual destination directory
$path = Split-Path (Join-Path $target $folder)
if (!(Test-Path $path)) {$null = mkdir $path}

# unzip c:\tmp\test.zip\test\etc\script to c:\tmp\output\test\etc\script
$shell.NameSpace($path).CopyHere($item)

Problem

I need to unzip a specific directory from a zipfile. Like for example extract the directory 'test\etc\script' from zipfile 'c:\tmp\test.zip' and place it in c:\tmp\output\test\etc\script. The code below works but has two quirks: I need to recursively find the directory ('script') in the zip file (function finditem) although I already know the path ('c:\tmp\test.zip\test\etc\script') With CopyHere I need to determine the targetdirectory, specifically the 'test\etc' part manually Any better solutions? Thanks. The code: ``` function finditem($items, $itemname) { foreach($item In $items) { if ($item.GetFolder -ne $Null) { finditem $item.GetFolder.items() $itemname } if ($item.name -Like $itemname) { return $item } } } $source = 'c:\tmp\test.zip' $target = 'c:\tmp\output' $shell = new-object -com shell.application # find script folder e.g. c:\tmp\test.zip\test\etc\script $item = finditem $shell.NameSpace($source).Items() "script" # output folder is c:\tmp\output\test\etc $targetfolder = Join-Path $target ((split-path $item.path -Parent) -replace '^.*zip') New-Item $targetfolder -ItemType directory -ErrorAction Ignore # unzip c:\tmp\test.zip\test\etc\script to c:\tmp\output\test\etc $shell.NameSpace($targetfolder).CopyHere($item) ```

Original source