Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Created February 25, 2016 05:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save nachivpn/3e53dd36120877d70aee to your computer and use it in GitHub Desktop.
Save nachivpn/3e53dd36120877d70aee 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"
@moreaki
Copy link

moreaki commented Sep 11, 2020

If the zip was created on a Unix system, you want to amend the check for directory to the following:

if (!$entryTargetFilePath.EndsWith("\") -And !$entryTargetFilePath.EndsWith("/")) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment