获取zip文件的未压缩大小。

3
我正在尝试自动化Windows上的Oracle数据库补丁,并且其中一个问题是验证服务器上是否有足够的空间来复制和提取补丁。
我需要一种快速获取zip归档(Oracle补丁)未压缩大小的方法,以确保脚本可以复制和提取补丁所需的空间充足。
我从这个线程开始: 7z get total size of uncompress contents? 根据samson7point1的答案,看起来很有前途:
'C:\Program Files\7-Zip\7z.exe' l '.\folder-with-zips\*.zip' | select-string -pattern "1 files" | Foreach {$size=[long]"$(($_ -split '\s+',4)[2])";$total+=$size;"$([math]::Round($total/1024/1024/1024,2))" + " GB"}

但以上方法对我来说似乎不起作用,可能是我做错了什么。
从这个帖子开始,我尝试在PowerShell中开发一些东西,可以求和7z的“大小”列,我得到了以下PowerShell脚本:
function getsizeofzip
{
$stopwatch = [System.Diagnostics.Stopwatch]::startNew()
$username = "yayo"
$password = ConvertTo-SecureString "yayo" -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($username, $password)
$masina = "yayo"
Write-Host Gathering list of zip files to calculate extract size -ForeGroundColor yellow
$alabala = Invoke-Command -ScriptBlock {Invoke-Expression "&'C:\Program Files\7-Zip\7z.exe' l -ba -slt Z:\Oracle\OraDB19c3\*.zip"} -Computer $masina -Credential $Credentials | Select-String "Size = "
Write-Host Saving output to file -ForegroundColor Yellow
$alabala >> vvv.txt
$total = 0
Write-Host Calculating extract size -ForeGroundColor yellow
for ( $index = 0; $index -lt $alabala.count; $index = $index + 2)
{
$temp = $alabala | Select-Object -Index $index
$size = $temp -replace "Size = " -replace ""
#$size
$total +=  ($size -as [int])
#Write-Host Current total $total
}
$stopWatch.Elapsed.TotalMinutes
$stopWatch.Stop()
Write-Host Totalul este ($total/1024/1024/1024)GB
}

输出如下:

Gathering list of zip files to calculate extract size 

Saving output to file Calculating extract size

21.5341170483333 

Totalul este 4.49708485417068 GB 

正如您所看到的,执行了以下7z命令:

'C:\Program Files\7-Zip\7z.exe' l -ba -slt Z:\Oracle\OraDB19c3\*.zip"

-ba -slt参数用于排序命令输出,至少要摆脱标题,我得到以下输出:

Path = 33808367\files\sqlpatch\33808367
Folder = +
Size = 0
Packed Size = 0
Modified = 2022-05-02 11:51:38
Created = 
Accessed = 
Attributes = D drwxr-xr-x
Encrypted = -
Comment = 
CRC = 
Method = Store
Characteristics = UT 0x7875
Host OS = Unix
Version = 10
Volume Index = 0
Offset = 251558554

Path = 33808367\files\sqlpatch\33808367\24758240
Folder = +
Size = 0
Packed Size = 0
Modified = 2022-05-01 13:25:40
Created = 
Accessed = 
Attributes = D drwxr-xr-x
Encrypted = -
Comment = 
CRC = 
Method = Store
Characteristics = UT 0x7875
Host OS = Unix
Version = 10
Volume Index = 0
Offset = 251558645

Path = 33808367\files\sqlpatch\33808367\24758240\rollback_files
Folder = +
Size = 0
Packed Size = 0
Modified = 2022-05-01 13:25:40
Created = 
Accessed = 
Attributes = D drwxr-xr-x
Encrypted = -
Comment = 
CRC = 
Method = Store
Characteristics = UT 0x7875
Host OS = Unix
Version = 10
Volume Index = 0
Offset = 251558745

Path = 33808367\files\sqlpatch\33808367\24758240\rollback_files\19.1.0.0.0
Folder = +
Size = 0
Packed Size = 0
Modified = 2022-05-01 13:25:40
Created = 
Accessed = 
Attributes = D drwxr-xr-x
Encrypted = -
Comment = 
CRC = 
Method = Store
Characteristics = UT 0x7875
Host OS = Unix
Version = 10
Volume Index = 0
Offset = 251558860

Path = 33808367\files\sqlpatch\33808367\24758240\rollback_files\19.1.0.0.0\javavm
Folder = +
Size = 0
Packed Size = 0
Modified = 2022-05-01 13:25:40
Created = 
Accessed = 
Attributes = D drwxr-xr-x
Encrypted = -
Comment = 
CRC = 
Method = Store
Characteristics = UT 0x7875
Host OS = Unix
Version = 10
Volume Index = 0
Offset = 251558986

....
...

由于我不知道如何格式化表格(我尝试过一点),以便仅选择Size列... 我将输出导向Select-String "Size =",现在输出如下:

Size = 0
Packed Size = 0
Size = 86652
Packed Size = 13750
Size = 0
Packed Size = 0
Size = 0
Packed Size = 0
Size = 0
Packed Size = 0
Size = 92973512
Packed Size = 12818027
Size = 0
Packed Size = 0
Size = 0
Packed Size = 0
Size = 205157781
Packed Size = 58369063
Size = 209589951
Packed Size = 59788028
Size = 0
Packed Size = 0
Size = 0
Packed Size = 0
Size = 0
Packed Size = 0
Size = 205157781

现在我循环遍历这个(index+2)来获取所有值并将它们相加。这可以工作,但是......
上面的脚本问题在于对于一个压缩大小为1.6G且包含数万个文件的Oracle补丁,需要23分钟...... 这有些太长了,因为我不太擅长powershell或优化,所以我正在寻求一些建议。
你能帮我指点一下方向吗?
-是否有更快的方法使用powershell排序7z输出? 我尝试将字符串输出格式化为表格以便更轻松地解析,但我没有成功,Format-Table(我理解这仅用于输出)或任何类似的cmdlets都没有帮助,默认情况下无法对表格进行排序... -也许还有另一个本机Windows实用程序可以显示zip文件的未压缩大小?
1个回答

2
尝试这个,它消除了编写和解析文本文件的要求,这可能是大部分时间消耗的地方。此解决方案将7zip命令的输出流传输到一个foreach对象。在foreach循环中,我们查找包含“Size =”的行,然后处理任何匹配项。
#Configure variable to hold our result
$size = 0

#Call 7zip and stream the output to a foreach object
&'C:\Program Files\7-Zip\7z.exe' l -bso0 -slt "C:\Temp\oracle.zip" | %{

    #Match the string "Size = anything" in 2 capture groups one of which will contain the filesize
    if($_ -match "(^Size\s=\s)(\d+$)"){
        
        #Add the uncompressed filesize to our result variable
        $size += $matches[2] -as [int]
    }
}

#Show the result in MB
$size / 1MB

2
完美,优化我的初始脚本后,只需将正确的行转储到变量中,然后对键进行求和,比起之前花费的1分钟,现在只用了20秒。非常感谢,我会使用你的代码 :P - Seke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接