我在此位置有单个或多个 zip 文件
C:\Temp\unzipped_files\
我有一个函数,它采用
$workspace
参数,如下所示:
Function RecursiveUnzip($workspace) {
$outputDir = "$workspace\unzipped_files"
$zips = Get-ChildItem -Path $outputDir -Filter "*.zip"
foreach ($zip in $zips) {
$markerFile = "$($zip.FullName).processed"
if (Test-Path -Path $markerFile) {
Write-Host "Skipping already processed ZIP: $($zip.FullName)"
} else {
Expand-Archive -Path $zip.FullName -DestinationPath $outputDir -Force
Set-Content -Path $markerFile -Value "Processed"
$nestedZips = Get-ChildItem -Path $outputDir -Recurse -Filter "*.zip"
foreach ($nestedZip in $nestedZips) {
RecursiveUnzip -workspace $workspace
}
}
}
}
$wspace = 'C:\Temp'
RecursiveUnzip -workspace $wspace
我需要选择
C:\Temp\unzipped_files\
内的每个*.zip并继续解压每个.zip文件,直到没有留下未在C:\Temp\unzipped_files\
下解压的.zip。
注意:正在提取的 .zip 中可能还包含其他也需要解压的 .zip。
我上面的尝试部分解压缩,但没有解压缩位于其他
.zip
文件中的那些 .zip 文件。
请推荐。
以下将能够递归扩展档案并与PowerShell 5.1兼容。 PowerShell 7+ 提供了
-PassThru
开关,可用于简化此任务。
注意: 代码将使用其父目录扩展嵌套的 Zip 存档,这是为了避免冲突。
# tweak this value if you want to overwrite (change to `$true`)
$overwrite = $false
$queue = [System.Collections.Generic.Queue[hashtable]]::new()
$queue.Enqueue(@{
# using a relative path to target the zip, change this value accordingly
LiteralPath = Convert-Path .\pwshbinary.zip
# using `pwd` to extract, change this value accordingly
DestinationPath = Join-Path $pwd myDestination
Force = $overwrite
})
while ($queue.Count) {
$current = $queue.Dequeue()
Expand-Archive @current
$hasZips = $current['DestinationPath'] | Get-ChildItem -Filter *.zip -Recurse
foreach ($zip in $hasZips) {
$queue.Enqueue(@{
LiteralPath = $zip.FullName
DestinationPath = Join-Path $zip.DirectoryName $zip.BaseName
Force = $overwrite
})
}
}