保存为1.bat,编码为ANSI;- <#*,:
- @echo off
- cd /d "%~dp0"
- set "batchfile=%~f0"
- Powershell -ExecutionPolicy Bypass -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::GetEncoding(0) )) )"
- pause
- exit /b
- #>
- $srcDir = "." # 原始文件路径 - 文件夹
- $dstDir = ".\解压后" # 解压后路径 - 文件夹
- $7z = "7z.exe" # 解压工具 - 7z
- $log = "xml解压日志.log" # 日志文件
-
-
- $di0 = [IO.Directory]::CreateDirectory($dstDir)
- $log = [IO.Path]::GetFullPath($log)
- $xml_reader_settings = New-Object System.Xml.XmlReaderSettings
- $xml_reader_settings.IgnoreWhitespace = $true
- $buff = New-Object 'byte[]' -ArgumentList 64kb
- Get-ChildItem -Path ("$srcDir\*" -replace '[\[\]]', '`$&') | Where-Object { $_ -is [IO.FileInfo] -and $_.FullName -ne $env:batchfile -and $_.FullName -ne $log } | ForEach-Object {
- $msg = "`r`n$(Get-Date -Format 'HH:mm') 开始处理,文件名为:$($_.Name)"
- $msg
- $msg | Out-Host
- $xml_reader = [System.Xml.XmlReader]::Create($_.FullName, $xml_reader_settings)
- if (-not $?) { return }
- $null = $xml_reader.MoveToContent()
- if ($xml_reader.NodeType -eq 'Element') {
- #第一种情况
- #$xml_reader.Name|Out-Host
- if ($xml_reader.Name -eq 'ZBFile' -and $xml_reader.ReadToDescendant('FileCont') -and $xml_reader.MoveToAttribute('Value')) {
- try {
- $msg = "$(Get-Date -Format 'HH:mm') 合并密文开始"
- $msg
- $msg | Out-Host
- $zipName = [Guid]::NewGuid().ToString() + '.zip'
- $zipPath = [IO.Path]::Combine($di0.FullName, $zipName)
- $zipStream = New-Object System.IO.FileStream -ArgumentList ($zipPath, [IO.FileMode]::Create, [IO.FileAccess]::Write, [IO.FileShare]::None)
- while (($readCount = $xml_reader.ReadContentAsBase64($buff, 0, $buff.Length)) -gt 0) {
- $zipStream.Write($buff, 0, $readCount)
- }
- $zipStream.Close()
- $zipStream = $null
- $msg = "$(Get-Date -Format 'HH:mm') 合并密文结束"
- $msg
- $msg | Out-Host
- $unzipDir = [IO.Path]::Combine($di0.FullName, $_.Name)
- $msg = "$(Get-Date -Format 'HH:mm') 解压文件开始"
- $msg
- $msg | Out-Host
- & $7z x -aoa -tzip -y "-o${unzipDir}" $zipPath | Out-Host
- $msg = "$(Get-Date -Format 'HH:mm') 解压文件结束"
- $msg
- $msg | Out-Host
- cmd /c ren $unzipDir ($_.BaseName) | Out-Host
- if ($?) {
- $msg = "$(Get-Date -Format 'HH:mm') 文件夹名字修改成功,修改为$($_.BaseName)"
- } else {
- $msg = "$(Get-Date -Format 'HH:mm') 文件夹名字修改失败: ${unzipDir} -> $($_.BaseName)"
- }
- $msg
- $msg | Out-Host
- Remove-Item -LiteralPath $zipPath
- } finally {
- if ($zipStream) { $zipStream.Close(); $zipStream = $null; }
- }
- trap {}
- } elseif ($xml_reader.Name -eq 'ConstructProject' -and $xml_reader.MoveToAttribute('UniqueCode')) {
- #第二种情况
- try {
- $pw = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($xml_reader.ReadContentAsString()))
- $msg = "$(Get-Date -Format 'HH:mm') 经过base64解密后的密码为${pw}"
- $msg
- $msg | Out-Host
- #$null=$xml_reader.MoveToElement()
- if ($xml_reader.ReadToFollowing('BidFileTable')) {
- $msg = "$(Get-Date -Format 'HH:mm') 合并密文开始"
- $msg
- $msg | Out-Host
- $zipName = [Guid]::NewGuid().ToString() + '.zip'
- $zipPath = [IO.Path]::Combine($di0.FullName, $zipName)
- $zipStream = New-Object System.IO.FileStream -ArgumentList ($zipPath, [IO.FileMode]::Create, [IO.FileAccess]::Write, [IO.FileShare]::None)
-
- while ($xml_reader.Read()) {
- if ($xml_reader.IsStartElement() -and $xml_reader.Name -match '^BidFile\d*$' -and $xml_reader.MoveToAttribute('Value')) {
- while (($readCount = $xml_reader.ReadContentAsBase64($buff, 0, $buff.Length)) -gt 0) {
- $zipStream.Write($buff, 0, $readCount)
- }
-
- }
- }
-
- $zipStream.Close()
- $zipStream = $null
- $msg = "$(Get-Date -Format 'HH:mm') 合并密文结束"
- $msg
- $msg | Out-Host
- $unzipDir = [IO.Path]::Combine($di0.FullName, $_.Name)
- $msg = "$(Get-Date -Format 'HH:mm') 解压文件开始"
- $msg
- $msg | Out-Host
- & $7z x "-p${pw}" -aoa -tzip -y "-o${unzipDir}" $zipPath | Out-Host
- $msg = "$(Get-Date -Format 'HH:mm') 解压文件结束"
- $msg
- $msg | Out-Host
- cmd /c ren $unzipDir ($_.BaseName)
- if ($?) {
- $msg = "$(Get-Date -Format 'HH:mm') 文件夹名字修改成功,修改为$($_.BaseName)"
- } else {
- $msg = "$(Get-Date -Format 'HH:mm') 文件夹名字修改失败: ${unzipDir} -> $($_.BaseName)"
- }
- $msg
- $msg | Out-Host
- Remove-Item -LiteralPath $zipPath
- }
- } finally {
- if ($zipStream) { $zipStream.Close(); $zipStream = $null; }
- }
- }
-
- }
- $xml_reader.Close()
-
- } | Set-Content -LiteralPath $log -Encoding UTF8
复制代码
|