2

我需要在磁盘中提取一系列 zip 文件。它们是很多数据,所以我需要验证是否有足够的可用空间。有没有办法使用 Powershell 查找 zip 文件内容的未压缩大小,而不需要解压缩它?这样我可以计算每个 zip 文件的未压缩大小,将它们相加并检查我的可用空间是否大于此值。

4

1 回答 1

4

这个函数可能会这样做:

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
于 2020-04-05T17:31:56.107 回答