我正在嘗試在Windows上自動執行 Oracle 資料庫補丁,我遇到的問題之一是驗證服務器上是否有足夠的空間來復制和提取補丁。
我需要一種快速獲取 zip 存檔(Oracle 補丁)的未壓縮大小的方法,以確保有足夠的空間供腳本復制和提取補丁。
我從這個執行緒開始:
7z 獲取解壓縮內容的總大小?
根據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(我知道這僅用于輸出)或任何類似的 cmdlet 都沒有幫助,默認情況下表格不可排序... -Is是否有另一個適用于 Windows 的本機實用程式可以顯示 zip 檔案的未壓縮大小?
uj5u.com熱心網友回復:
試試這個,它消除了撰寫然后決議文本檔案的要求,這可能是大部分時間花費的地方。此解決方案將 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487513.html
