我有一个.zip文件,需要使用Powershell解压缩其全部内容。我在这么做,但似乎并不奏效:
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\a").CopyHere($item)
}
怎么了?目录C:\a仍然为空。
我有一个.zip文件,需要使用Powershell解压缩其全部内容。我在这么做,但似乎并不奏效:
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
$shell.Namespace("C:\a").CopyHere($item)
}
怎么了?目录C:\a仍然为空。
当前回答
使用powershell内置的方法Expand-Archive
例子
Expand-Archive -LiteralPath C:\archive.zip -DestinationPath C:\
其他回答
嘿,它为我工作..
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("put ur zip file path here")
foreach ($item in $zip.items()) {
$shell.Namespace("destination where files need to unzip").CopyHere($item)
}
使用expand-archive,但自动创建以存档命名的目录:
function unzip ($file) {
$dirname = (Get-Item $file).Basename
New-Item -Force -ItemType directory -Path $dirname
expand-archive $file -OutputPath $dirname -ShowProgress
}
ForEach循环处理位于$filepath变量中的每个ZIP文件
foreach($file in $filepath)
{
$zip = $shell.NameSpace($file.FullName)
foreach($item in $zip.items())
{
$shell.Namespace($file.DirectoryName).copyhere($item)
}
Remove-Item $file.FullName
}
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)
}
}
使用:
unzip 'Applications\Site.zip' 'C:\inetpub\wwwroot\Site'
对于那些想要使用Shell.Application.Namespace.Folder.CopyHere()并且想要在复制时隐藏进度条,或者使用更多选项的人,文档在这里: https://learn.microsoft.com/en-us/windows/desktop/shell/folder-copyhere
要使用powershell和隐藏进度条并禁用确认,您可以使用如下代码:
# We should create folder before using it for shell operations as it is required
New-Item -ItemType directory -Path "C:\destinationDir" -Force
$shell = New-Object -ComObject Shell.Application
$zip = $shell.Namespace("C:\archive.zip")
$items = $zip.items()
$shell.Namespace("C:\destinationDir").CopyHere($items, 1556)
Shell的使用限制。应用程序在windows核心版本: https://learn.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core
在windows核心版本中,默认情况下不会安装Microsoft-Windows-Server-Shell-Package,因此shell。应用程序将无法工作。
注意:以这种方式提取档案会花费很长时间,并且会降低windows gui的速度