gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:07:05 25 4
gpt4 key购买 nike

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

最佳答案

此功能可以做到:

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

关于powershell - 使用Powershell获取许多zip文件的未压缩大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com