gpt4 book ai didi

powershell - 获取未压缩的 zip 大小

转载 作者:行者123 更新时间:2023-12-05 04:28:44 25 4
gpt4 key购买 nike

我正在尝试在 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 参数用于排序命令输出,至少去掉 header ,我得到以下输出:

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 和 10 万个文件的 Oracle 补丁,它需要……23 分钟……这在某种程度上太多了,因为我不太擅长powershell 或一般优化我正在寻找一些建议。

你能帮我指出正确的方向吗:

-是否有更快的方法来使用 powershell 对 7z 输出进行排序?我尝试将字符串输出格式化为表格以便于解析,但我没有成功,Format-Table(我知道这仅用于输出)或任何类似的 cmdlet 都没有帮助,默认情况下表格不可排序 ...- 是否有另一个适用于 Windows 的 native 实用程序可以显示 zip 文件的未压缩大小?

最佳答案

试试这个,它消除了编写然后解析文本文件的要求,这可能是花费大部分时间的地方。此解决方案将 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

关于powershell - 获取未压缩的 zip 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72542845/

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