Created
          February 25, 2016 05:01 
        
      - 
      
 - 
        
Save nachivpn/3e53dd36120877d70aee to your computer and use it in GitHub Desktop.  
    Unzip a file in powershell by overwriting existing files
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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
  
            
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("/")) {