【问题标题】:Expand-Archive in powershell is failing to extract nested folders and filesPowershell中的Expand-Archive无法提取嵌套文件夹和文件
【发布时间】:2021-11-05 18:52:41
【问题描述】:

我有以下简单的 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 更改为不同的文件夹会怎样?
  • 不幸的是没有帮助
  • 你能用7z e吗?有一个 powershell script 用于运行它。
  • 可能是您的存档存在问题:github.com/PowerShell/Microsoft.PowerShell.Archive/issues/12 这个问题现在似乎已经修复,因此如果您运行的是旧版本,您可能需要更新 Powershell。
  • 我使用的是 powershell 5.1。我已经设法使用 7z e 拼凑出一个解决方案

标签: powershell


【解决方案1】:

我过去和一位同事遇到过这个模块的问题,我拼凑了以下内容

# 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
}

也许这有点用?

【讨论】:

【解决方案2】:

这是怎么回事?

Expand-Archive 在尝试扩展某些文件时失败(在我的情况下是由于路径太长)并尝试删除它认为已提取的文件(请参阅https://github.com/PowerShell/Microsoft.PowerShell.Archive/blob/master/Microsoft.PowerShell.Archive/Microsoft.PowerShell.Archive.psm1#L418),但Remove-Item 可以'找不到任何文件,因为它们实际上并未被提取。

切换到 PowerShell 7 为我解决了长路径问题。

【讨论】:

    【解决方案3】:

    在命令中添加 -force 对我有用。

    【讨论】:

      【解决方案4】:

      我得到了同样的错误,当我删除 ZIP 文件路径的双引号时它对我有用。

      例如:

      $FolderPath = "C:\Temp\Whatever"
      

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

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题。 当导出的文件路径过长时会出现此问题。

        【讨论】:

        • 我在问题中没有看到任何“太长”路径的确认。
        【解决方案6】:

        我对长路径没有任何问题 - 这是其他问题 - 不确定是什么。我运行的是 Windows Server 2019,默认的 powershell 是 v5.1(您可以通过$PSVersionTable 查询)。

        我安装了 v7.1 和 v5.1。然后我可以使用powershell 命令运行powershell v5,使用pwsh 命令运行v7.1。 Expand-Archive 现在可以正常工作了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多