【问题标题】:PowerShell Extract from zip file 17 folders deepPowerShell 从 zip 文件中提取 17 个文件夹深
【发布时间】:2017-09-02 09:26:01
【问题描述】:

我有一个自动创建的 zip 文件,我无法更改其中的文件夹数量。

我正在尝试从 zip 文件中 17 个文件夹深处的文件夹中提取所有内容。问题是文件夹的名称可以更改。

我开始使用 7Zip 解压另一个 zip 文件夹,效果很好:

$zipExe = join-path ${env:ProgramFiles(x86)} '7-zip\7z.exe'
if (-not (test-path $zipExe)) {
    $zipExe = join-path ${env:ProgramW6432} '7-zip\7z.exe'
    if (-not (test-path $zipExe)) {
         '7-zip does not exist on this system.'
    }
}
set-alias zip "C:\Program Files\7-Zip\7z.exe"
zip x $WebDeployFolder -o \$WebDeployTempFolder 

有没有办法提取 zip 文件中 17 个文件夹深处的文件夹中的内容?

【问题讨论】:

  • 我错过了您所面临的问题或问题:您可以编辑您的问题,以便清楚地说明您面临的问题吗?
  • @bluuf 完成。已添加所以我的问题是:有没有办法提取 zip 文件中 17 个文件夹深处的文件夹中的内容。
  • @bluuf 我的回答是:是的。请澄清你的问题。具体问题是什么?你自己试过吗?在帮助中心查看How do I ask a good question?
  • 我想您在使用 Powershell 的本机功能的 Windows 上遇到了最大路径长度问题?
  • @jessehouwing 不,我只是不确定如何在 powershell 中编写它。那么我应该做 StartFolder**** 之类的事情吗?

标签: powershell powershell-4.0 powershell-5.0


【解决方案1】:

您可以使用 7Zip 的列表功能来获取文件的内容。然后您可以解析该输出,查找具有 17 个级别的文件夹并使用该路径来提取内容。

下面是执行此操作的一段代码。

$7zip = "${env:ProgramFiles(x86)}\7-Zip\7z.exe"
$archiveFile = "C:\Temp\Archive.zip"
$extractPath = "C:\Temp"
$archiveLevel = 17

# Get contents list from zip file
$zipContents = & $7zip l $archiveFile

# Filter contents for only folders, described as "D" in Attr column
$contents = $zipContents | Where-Object { $_ -match "\sD(\.|[A-Z]){4}\s"}

# Get line where the folder level defined in $archiveLevel is present
$folderLine = $contents | Where-Object { ($_ -split "\\").Count -eq ($archiveLevel) }

# Get the folder path from line
$folderPath = $folderLine -split "\s" | Where-Object { $_ } | Select-Object -Last 1

# Extract the folder to the desired path. This includes the entire folder tree but only the contents of the desired folder level
Start-Process $7zip -ArgumentList "x $archiveFile","-o$extractPath","$folderPath" -Wait

# Move the contents of the desired level to the top of the path
Move-Item (Join-Path $extractPath $folderPath) -Destination $extractPath

# Remove the remaining empty folder tree
Remove-Item (Join-Path $extractPath ($folderPath -split "\\" | Select-Object -First 1)) -Recurse

代码中有几个注意事项。 如果没有完整路径/参数,我找不到仅提取文件夹的方法。所以最后被清理干净了。但请注意,父文件夹不包含任何其他文件或文件夹。 另外,我必须在最后使用“Start-Process”,否则 7Zip 会破坏变量输入。

您可能需要根据您的 ZIP 文件结构对其进行一些更改,但它应该能让您继续前进。

【讨论】:

  • 我在本地进行了人工测试,它对我有用,但您的环境绝对可能存在一些差异。你还记得更新顶部的路径吗?你能提供确切的错误信息吗?
  • 是的,我已经更改了路径,我应该添加这些是网络路径。错误:无效路径:无效路径:'\\MachineName\shared\GDistribute\Test\QA\WebsiteName\CodeName.Web\Content\R_C\GO47_1\DATA01\2\CodeName\BuildName\Sources\src\DIR\31x\Source \CodeName.Web\obj\Release\Package\PackageTmp'。在 line:22 char:1 + Move-Item (Join-Path $extractPath $folderPath) -Destination $extractPath + CategoryInfo : InvalidOperation: (:) [], ArgumentException + FullyQualifiedErrorId : MoveItemDynamicParametersProviderException
  • 我看不出这不起作用的原因,但这可能是提供商问题。尝试将 Move-Item 项目行更改为: Move-Item "filesystem::$(Join-Path $extractPath $folderPath)" -Destination "filesystem::$extractPath"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2017-04-12
  • 1970-01-01
相关资源
最近更新 更多