如何在Powershell中解压缩文件?

Uli*_*kel 213 powershell

我有一个.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)
}
Run Code Online (Sandbox Code Playgroud)

怎么了?该目录C:\a仍为空.

Kei*_*ill 470

在PowerShell v5 +中,内置了一个Expand-Archive命令(以及Compress-Archive):

Expand-Archive c:\a.zip -DestinationPath c:\a
Run Code Online (Sandbox Code Playgroud)

  • 使用`$ PSVersionTable.PSVersion`来确定您正在运行的PowerShell版本. (25认同)
  • 看起来参数`OutputPath`已经改为`DestinationPath`(参考https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.archive/Expand-Archive) (11认同)
  • @Ghashange PowerShell 5甚至在发布此答案时甚至不作为预发布版本的Windows 10和Server 2012以外的任何内容都可用. (2认同)
  • 您还可以使用相对路径,如`Expand-Archive -Path .\a.zip -DestinationPath .` (2认同)
  • 请注意,“扩展存档”和“压缩存档”要求存档文件扩展名为“.zip”,而基于“[System.IO.Compression.ZipFile]”的解决方案则不需要。 (2认同)

Mic*_*lli 232

以下是使用System.IO.Compression.ZipFile中的ExtractToDirectory的简单方法:

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"
Run Code Online (Sandbox Code Playgroud)

请注意,如果目标文件夹不存在,ExtractToDirectory将创建它.

如何压缩和提取文件

  • 从理论上讲,你没有.我试图在函数中隐藏复杂/非常规调用,以便稍后我可以替换方法而不必担心它的使用位置.正如Keith所说,在V5中将有一种新方法. (17认同)
  • 为什么要创建一个函数来替换单个函数调用? (9认同)
  • 我尝试了这个但是得到了以下错误,`Exception调用"ExtractToDirectory"并带有"2"参数:"无法找到中央目录记录的结尾." 在行:5 char:5 + [System.IO.Compression.ZipFile] :: ExtractToDirectory($ zipfile,$ ou ... + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +类别信息:未指定: (:) [],MethodInvocationException + FullyQualifiedErrorId:InvalidDataException` (9认同)
  • 这给了我以下错误:`Add-Type:无法添加类型.无法找到程序集"System.IO.Compression.FileSystem".我安装了.NET 4.6.2,并且我已经验证了程序集在GAC中,但我没弄清楚为什么我收到此错误. (4认同)

NIK*_*NIK 24

在PowerShell v5.1中,与v5相比略有不同.根据MS文档,它必须有一个-Path参数来指定存档文件路径.

Expand-Archive -Path Draft.Zip -DestinationPath C:\Reference
Run Code Online (Sandbox Code Playgroud)

或者,这可能是一个实际的路径:

Expand-Archive -Path c:\Download\Draft.Zip -DestinationPath C:\Reference
Run Code Online (Sandbox Code Playgroud)

展开 - 存档文档

  • 此Cmdlet中的v5和v5.1之间没有区别。您无需命名第一个参数。它会自动成为路径。例如,“ Expand-Archive Draft.Zip -DestinationPath C:\ Reference”可以正常工作。另外,它不是“实际”路径,而是“绝对”路径。 (2认同)

Kol*_*yon 13

使用内置的powershell方法Expand-Archive

例子

Expand-Archive -LiteralPath C:\archive.zip -DestinationPath C:\
Run Code Online (Sandbox Code Playgroud)


小智 12

嘿它为我工作..

$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)
}
Run Code Online (Sandbox Code Playgroud)

  • 不适用于Windows 2012/2016的"核心"版本 (2认同)
  • 如果其中一个文件或目录已经存在于目标位置,它会弹出一个对话框,询问该怎么做(忽略,覆盖),这会破坏目的.有谁知道如何强制它以静默覆盖? (2认同)

Sal*_*deh 7

Expand-Archivecmdlet与参数集之一一起使用:

Expand-Archive -LiteralPath C:\source\file.Zip -DestinationPath C:\destination
Run Code Online (Sandbox Code Playgroud)
Expand-Archive -Path file.Zip -DestinationPath C:\destination
Run Code Online (Sandbox Code Playgroud)


mik*_*ana 5

使用expand-archive但自动创建以存档命名的目录:

function unzip ($file) {
    $dirname = (Get-Item $file).Basename
    New-Item -Force -ItemType directory -Path $dirname
    expand-archive $file -OutputPath $dirname -ShowProgress
}
Run Code Online (Sandbox Code Playgroud)

  • 大多数归档程序将解压到以归档文件命名的目录中,与归档文件位于同一位置。如果您想要不同的东西,没有什么可以阻止您添加参数,但这是一个合理的默认值。 (2认同)

小智 5

对于那些想要使用Shell.Application.Namespace.Folder.CopyHere()并希望在复制时隐藏进度条或使用更多选项的人,文档位于: https:
//learn.microsoft.com/en-us /windows/desktop/shell/文件夹-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)
Run Code Online (Sandbox Code Playgroud)

Windows Core 版本上使用 Shell.Application 的限制:
https://learn.microsoft.com/en-us/windows-server/administration/server-core/what-is-server-core

在 Windows核心版本上,默认情况下未安装Microsoft-Windows-Server-Shell-Package,因此 shell.applicaton 将不起作用。

注意:以这种方式提取档案将花费很长时间,并且会减慢 Windows GUI 的速度