Skip to content

Instantly share code, notes, and snippets.

@jayrgee
Forked from nachivpn/unzip.ps1
Created October 11, 2018 00:06
Show Gist options
  • Save jayrgee/0c0022bbf5e10c2140474be417f21ac7 to your computer and use it in GitHub Desktop.
Save jayrgee/0c0022bbf5e10c2140474be417f21ac7 to your computer and use it in GitHub Desktop.
Unzip a file in powershell by overwriting existing files
function Unzip($zipfile, $outdir)
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
$archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
foreach ($entry in $archive.Entries)
{
$entryTargetFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
$entryDir = [System.IO.Path]::GetDirectoryName($entryTargetFilePath)
#Ensure the directory of the archive entry exists
if(!(Test-Path $entryDir )){
New-Item -ItemType Directory -Path $entryDir | Out-Null
}
#If the entry is not a directory entry, then extract entry
if(!$entryTargetFilePath.EndsWith("\")){
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entryTargetFilePath, $true);
}
}
}
Unzip -zipfile "$zip" -outdir "$dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment