如何在Powershell中解压缩文件? [英] How to unzip a file in Powershell?

查看:177
本文介绍了如何在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:

另见:

  • How to Compress and Extract files (Microsoft Docs)

这篇关于如何在Powershell中解压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆