How to extract a specific folder from a zip file in powershell

Hi,

I have a zip file contains folders, sub folders and files.

I want to extract a specific sub folder along with files and folders in it.

Can anyone suggest a way to accomplish this. I tried the below code but no luck.

$xtrctfolder = “Valueadd\Data\Current\admin\production”
$destination = “D:\Dinesh\Final_xtract”

Add-Type -Assembly System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($sourcePath)
$zip.Entries | where {$_.Name -eq $xtrctfolder} | foreach {

$target_path 	= [System.IO.Path]::Combine($destination, $_.Name)
$target_folder 	= [System.IO.Path]::GetDirectoryName($target_path)

if(!(Test-Path $target_folder ))
{
    New-Item -ItemType Directory -Path $target_folder | Out-Null 
}

if(!$target_path.EndsWith("\"))
{
            [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $target_path, $true);
}

}

Thanks
Dinesh

Hi Dinesh!

Welcome to the community :slight_smile: This is a pretty generic PowerShell question and not directly related to Octopus and therefore would be better suited on a forum like StackOverflow. Having said that, I gave it a crack on my local machine and the snippet below should get you unblocked :+1:.

Add-Type -AssemblyName System.IO.Compression.FileSystem

$zip = [IO.Compression.ZipFile]::OpenRead("path/to/example.zip")
$entries = $zip.Entries | Where-Object { $_.FullName -eq "path/to/file/in/zip/example1.txt" -and $_FullName -eq "path/to/another/file/in/zip/example2.txt" }
$entries | ForEach-Object { [IO.Compression.ZipFileExtensions]::ExtractToFile($_, "path/to/extract/file/" + $_.Name) }