使用 powershell 获取许多 zip 文件的未压缩大小
Get uncompressed size of many zip files with powershell
我需要提取磁盘中的一系列 zip 文件。它们有很多数据,所以我需要验证是否有足够的可用数据 space。有没有办法在不解压缩的情况下使用 Powershell 查找 zip 文件内容的未压缩大小?这样我就可以计算每个 zip 文件的未压缩大小,对它们求和并检查我的可用 space 是否大于这个值。
这个函数可以做到:
function Get-UncompressedZipFileSize {
param (
$Path
)
$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace($Path)
$size = 0
foreach ($item in $zip.items()) {
if ($item.IsFolder) {
$size += Get-UncompressedZipFileSize -Path $item.Path
} else {
$size += $item.size
}
}
# It might be a good idea to dispose the COM object now explicitly, see comments below
[System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$shell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
return $size
}
用法示例:
$zipFiles = Get-ChildItem -Path "C:\path\to\zips" -Include *.zip -Recurse
foreach ($zipFile in $zipFiles) {
Select-Object @{n='FullName'; e={$zipFile.FullName}}, @{n='Size'; e={Get-UncompressedZipFileSize -Path $zipFile.FullName}} -InputObject ''
}
示例输出:
FullName Size -------- ---- C:\test1.zip 4334400 C:\test2.zip 8668800 C:\test3.zip 8668800