如何在Powershell中解压缩文件? [英] How to unzip a file in Powershell?
本文介绍了如何在Powershell中解压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 .zip
文件,需要使用 Powershell 解压其全部内容.我正在这样做,但似乎不起作用:
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 -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
仍然是空的.
What's wrong? The directory C:\a
is still empty.
推荐答案
这是使用 ExtractToDirectory 来自 System.IO.Compression.ZipFile:
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "C:\a.zip" "C:\a"
请注意,如果目标文件夹不存在,ExtractToDirectory 将创建它.其他注意事项:
Note that if the target folder doesn't exist, ExtractToDirectory will create it. Other caveats:
- 现有文件不会被覆盖,而是会触发 IOException.
- 此方法至少需要 .NET Framework 4.5,可用于 Windows Vista 和更高版本.
- 不会根据当前工作目录解析相对路径,请参阅 为什么 PowerShell 中的 .NET 对象不使用当前目录?
另见:
- 如何压缩和提取文件 (Microsoft Docs)
- How to Compress and Extract files (Microsoft Docs)
这篇关于如何在Powershell中解压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文