获取未压缩的zip大小



我正在尝试在Windows上自动修补Oracle数据库我遇到的一个问题是要验证服务器上是否有足够的空间来复制和提取补丁。

我需要的是一种快速的方法来获得zip存档(Oracle补丁)的未压缩大小,以确保有足够的空间供脚本复制和提取补丁。

我从这个线程开始:

7z得到解压内容的总大小?

根据samson7point1

,看起来很有希望的回答是:

'C:Program Files7-Zip7z.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中开发一些东西,它将"大小"one_answers";然后输入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 Files7-Zip7z.exe' l -ba -slt Z:OracleOraDB19c3*.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 Files7-Zip7z.exe' l -ba -slt Z:OracleOraDB19c3*.zip"

-ba -slt参数用于对命令输出进行排序,至少可以去掉头,我得到以下输出:

Path = 33808367filessqlpatch33808367
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 = 33808367filessqlpatch3380836724758240
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 = 33808367filessqlpatch3380836724758240rollback_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 = 33808367filessqlpatch3380836724758240rollback_files19.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 = 33808367filessqlpatch3380836724758240rollback_files19.1.0.0.0javavm
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
....
...

因为我不知道如何格式化表(我尝试了一点),以便只选择大小列…我将输出通过管道传输到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

现在我循环遍历this (index+2)以获得所有值并将它们相加。

以上脚本的问题是,对于一个压缩大小为1.6g的Oracle补丁和成千上万的文件,它需要…23分钟……这有点太多了,因为我不是很好与powershell或优化一般我正在寻找一些建议。

你能告诉我正确的方向吗?

-是否有一种更快的方法来排序7z输出与powershell?我尝试将字符串输出格式化为表格以方便解析,但我没有成功,Format-Table(我理解这仅用于输出)或任何类似的cmdlet都没有帮助,表默认情况下不可排序…是否有其他的Windows本地实用程序可以显示未压缩的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 Files7-Zip7z.exe' l -bso0 -slt "C:Temporacle.zip" | %{
#Match the string "Size = anything" in 2 capture groups one of which will contain the filesize
if($_ -match "(^Sizes=s)(d+$)"){

#Add the uncompressed filesize to our result variable
$size += $matches[2] -as [int]
}
}
#Show the result in MB
$size / 1MB

最新更新