PowerShell 从 17 个文件夹深处的 zip 文件中提取
PowerShell Extract from zip file 17 folders deep
我有一个自动创建的 zip 文件,我无法更改其中的文件夹数量。
我正在尝试从 zip 文件中包含 17 个文件夹的文件夹中提取所有内容。问题是文件夹的名称可以更改。
我开始使用 7Zip 提取另一个 zip 文件夹并且工作正常:
$zipExe = join-path ${env:ProgramFiles(x86)} '7-zipz.exe'
if (-not (test-path $zipExe)) {
$zipExe = join-path ${env:ProgramW6432} '7-zipz.exe'
if (-not (test-path $zipExe)) {
'7-zip does not exist on this system.'
}
}
set-alias zip "C:\Program Files-Zipz.exe"
zip x $WebDeployFolder -o $WebDeployTempFolder
有没有办法提取zip文件中17个文件夹深处的文件夹中的内容?
您可以使用7Zip的列表功能来获取文件的内容。然后您可以解析该输出,查找具有 17 层的文件夹并使用该路径提取内容。
下面是执行此操作的一段代码。
zip = "${env:ProgramFiles(x86)}-Zipz.exe"
$archiveFile = "C:\Temp\Archive.zip"
$extractPath = "C:\Temp"
$archiveLevel = 17
# Get contents list from zip file
$zipContents = & zip 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 zip -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
代码中有几个注意事项。
如果没有完整 path/parensts,我找不到只提取文件夹的方法。所以最后清理干净了。但请注意,父文件夹不包含任何其他文件或文件夹。
另外,我必须在最后使用 "Start-Process" 否则 7Zip 会破坏变量输入。
根据您的 ZIP 文件结构,您可能需要稍微更改它,但它应该可以让您继续。
我有一个自动创建的 zip 文件,我无法更改其中的文件夹数量。
我正在尝试从 zip 文件中包含 17 个文件夹的文件夹中提取所有内容。问题是文件夹的名称可以更改。
我开始使用 7Zip 提取另一个 zip 文件夹并且工作正常:
$zipExe = join-path ${env:ProgramFiles(x86)} '7-zipz.exe'
if (-not (test-path $zipExe)) {
$zipExe = join-path ${env:ProgramW6432} '7-zipz.exe'
if (-not (test-path $zipExe)) {
'7-zip does not exist on this system.'
}
}
set-alias zip "C:\Program Files-Zipz.exe"
zip x $WebDeployFolder -o $WebDeployTempFolder
有没有办法提取zip文件中17个文件夹深处的文件夹中的内容?
您可以使用7Zip的列表功能来获取文件的内容。然后您可以解析该输出,查找具有 17 层的文件夹并使用该路径提取内容。
下面是执行此操作的一段代码。
zip = "${env:ProgramFiles(x86)}-Zipz.exe"
$archiveFile = "C:\Temp\Archive.zip"
$extractPath = "C:\Temp"
$archiveLevel = 17
# Get contents list from zip file
$zipContents = & zip 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 zip -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
代码中有几个注意事项。 如果没有完整 path/parensts,我找不到只提取文件夹的方法。所以最后清理干净了。但请注意,父文件夹不包含任何其他文件或文件夹。 另外,我必须在最后使用 "Start-Process" 否则 7Zip 会破坏变量输入。
根据您的 ZIP 文件结构,您可能需要稍微更改它,但它应该可以让您继续。