在PowerShell中使用Expand-Archive无法提取嵌套文件夹和文件

12

我有以下简单的PowerShell脚本,用于将一个zip文件夹(包含其他文件夹和仅日志文件)提取到目标位置。

$FolderPath = "C:\Temp\Whatever"

Expand-Archive -Path "$FolderPath\logs.zip" -DestinationPath "$FolderPath\logs"

不幸的是,这会返回一堆像下面这样的错误...

Remove-Item : Cannot find path 'C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Agent.log' because it does not exist.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:410 char:46
+ ...                 $expandedItems | % { Remove-Item $_ -Force -Recurse }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp\Whateve...alize Agent.log:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find path 'C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Job.log' because it does not exist.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:410 char:46
+ ...                 $expandedItems | % { Remove-Item $_ -Force -Recurse }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Temp\Whateve...tialize Job.log:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

还有许多类似的错误。

我可以确认,第一个错误中引用的文件C:\Temp\Whatever\logs\1_Selenium SEPA-Test\Attempt1\1_Start VM's\Release\1_Initialize Agent.log在等效位置的zip文件夹中确实存在...

输入图像描述

脚本完成后,我在指定目录中看到了一个不完整的文件夹。

输入图像描述

这里发生了什么?

谢谢,


如果您只是将“-DestinationPath”更改为另一个文件夹,会发生什么? - trebleCode
很遗憾,这并没有帮助。 - Konzy262
1
你能使用7z e吗?有一个powershell脚本可以运行它。 - lloyd
可能存在您的存档问题:https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/12。此问题似乎已经得到解决,因此如果您正在运行旧版本,则可能需要更新Powershell。 - henrycarteruk
我正在使用 PowerShell 5.1。我已经成功地拼凑出了一个解决方案,使用 7z e 命令。 - Konzy262
9个回答

8

我过去曾经遇到过这个模块的问题,和一个同事一起凑巧想出了以下解决方法

# This script was created to extract the contents of multiple ZIP files located in a directory
# structure. Each ZIP files is extracted within the folder it resides.

# File path
$filepath = Get-ChildItem -Path 'C:\Users\Luke\Desktop\ArchivedScripts\' -Filter *.zip -Recurse

# convert filepath to NameSpace object
$shell = new-object -com shell.application

# ForEach Loop processes each ZIP file located within the $filepath variable
foreach($file in $filepath)
{
    $zip = $shell.NameSpace($file.FullName)
    foreach($item in $zip.items())
    {
        $shell.Namespace($file.DirectoryName).copyhere($item)
    }
    Remove-Item $file.FullName
}

也许这对你有所帮助?

很棒的解决方案!这里可以找到一个简化的函数来处理一个ZIP文件:https://serverfault.com/a/201604 - R Yoda
我想要更多的解释。我猜这是打开一个Windows Shell会话,然后使用它来探索zip文件的内容并将它们从zip文件中复制出来(在此过程中扩展它们)。这是否通过依赖于Windows Shell对长文件路径的支持来处理长文件路径问题,或者是通过Expand-Archive解决不同的问题? - Tydaeus

7

3

我也遇到了同样的错误,当我删除ZIP文件路径中的双引号时,它就可以正常工作。

例如:

Original Answer翻译成"最初的回答"

$FolderPath = "C:\Temp\Whatever"

将$FolderPath\logs.zip文件解压到"$FolderPath\logs"目录中,可使用Expand-Archive命令。


哇 - 这对我有用!我在运行此命令 Expand-Archive -LiteralPath "$downloadFile" -DestinationPath "$Destination" 时遇到以下错误: "Exception calling "ExtractToFile" with "3" argument(s): "The archive entry was compressed using an unsupported compression method.".Message" - 我从 "$downloadFile" 中删除了双引号,然后它就起作用了!谢谢... - Tom

2
我遇到了同样的问题。这个问题出现在需要导出的文件路径太长的情况下。最初的回答。

1
我在问题中没有看到任何“路径过长”的确认。 - Anton Menshov

2
将命令中添加“-force”对我很有效。最初的回答。

1

这是我在2016 Server(PS 5.1)中使Expand-Archive工作的唯一方法。但是,当我尝试在本地运行完全相同的命令时,它彻底失败了...

#copy_and_expandarchive.ps1
$ServerList = gc D:\batch\input\servers.txt
Foreach ($server in $ServerList){
if(Test-Connection $server -count 1 -quiet){
    if($server -like '#*')
    {
        continue
    }
    Invoke-Command $server -ScriptBlock {
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Expand-Archive -force -verbose –LiteralPath 'D:\install\logstash-7-17-3GIS-Prod.zip' –Destination 'd:\apps'
    }}}

为什么这个可以工作呢?因为在这个镜像中,默认使用x86版本的PowerShell作为命令解释器(64位PS:C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe只有在组策略成功后才会应用 - 我必须禁用IPv6才能完成并运行gpupdate / target:computer,使用saps-wait确保它有效)


1

我在处理长路径时没有遇到任何问题,但还是存在其他问题,不确定是什么原因。我使用的是Windows Server 2019并默认安装了PowerShell v5.1(您可以使用$PSVersionTable 命令进行确认)。

我安装了v7.1版本并将其与v5.1一起运行。现在我可以使用powershell命令来运行v5,并使用pwsh命令来运行v7.1。 现在Expand-Archive命令可以正常工作了。


0

我在另一种情况下遇到了同样的错误。
我正在尝试从另一台计算机解压缩一个编码不同的Zip文件。

解决方案1:保持相同的编码对我有用。
设置:设置-->语言&地区-->地区-->当前系统区域设置。
注意:不要勾选测试版:使用Unicode UTF-8支持全球语言。

解决方案2:使用其他工具,如Powershell77z.exe


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
顺便提一下,解决这个问题的另一种方法是提供一个错误的目标路径,例如像这样(是的,我犯过这个错误!)。
$variable1="c:\a\path\somewhere"
...
# Note the errant space before the last quote below!
$variable2="${variable1} "
...
Expand-Archive ... -DestinationPath "${variable2}"...

所以我的目标路径现在不存在,你会遇到与原帖中相同的症状。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接