使用powershell获取许多zip文件的未压缩大小

问题描述 投票:2回答:1

我需要在磁盘中提取一系列zip文件。它们包含大量数据,因此我需要验证是否有足够的可用空间。有没有一种方法可以使用Powershell在不解压缩的情况下找到zip文件内容的未压缩大小?这样,我可以计算每个zip文件的未压缩大小,对其求和,然后检查我的可用空间是否大于此值。

powershell zip filesize
1个回答
3
投票

此功能可以做到:

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
© www.soinside.com 2019 - 2024. All rights reserved.