How to unzip a file in Powershell?

前端 未结 9 2140
清歌不尽
清歌不尽 2020-11-29 17:07

I have a .zip file and need to unpack its entire content using Powershell. I\'m doing this but it doesn\'t seem to work:

$shell = New-Object -Co         


        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 17:09

    function unzip {
        param (
            [string]$archiveFilePath,
            [string]$destinationPath
        )
    
        if ($archiveFilePath -notlike '?:\*') {
            $archiveFilePath = [System.IO.Path]::Combine($PWD, $archiveFilePath)
        }
    
        if ($destinationPath -notlike '?:\*') {
            $destinationPath = [System.IO.Path]::Combine($PWD, $destinationPath)
        }
    
        Add-Type -AssemblyName System.IO.Compression
        Add-Type -AssemblyName System.IO.Compression.FileSystem
    
        $archiveFile = [System.IO.File]::Open($archiveFilePath, [System.IO.FileMode]::Open)
        $archive = [System.IO.Compression.ZipArchive]::new($archiveFile)
    
        if (Test-Path $destinationPath) {
            foreach ($item in $archive.Entries) {
                $destinationItemPath = [System.IO.Path]::Combine($destinationPath, $item.FullName)
    
                if ($destinationItemPath -like '*/') {
                    New-Item $destinationItemPath -Force -ItemType Directory > $null
                } else {
                    New-Item $destinationItemPath -Force -ItemType File > $null
    
                    [System.IO.Compression.ZipFileExtensions]::ExtractToFile($item, $destinationItemPath, $true)
                }
            }
        } else {
            [System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($archive, $destinationPath)
        }
    }
    

    Using:

    unzip 'Applications\Site.zip' 'C:\inetpub\wwwroot\Site'
    

提交回复
热议问题