This is a script for packaging files and calculating MD5Sum as following steps.
- Get 3 parameters working folder, zip file name, and target file name
- Check if archive tool(7zip/WinRAR) existed in the working folder
- Get 7zip command from my FTP server
- Check if file/folder existed
- Archive target file into a zip file
- Calculate MD5Sum of the zip file
#Installer package
$DebugPreference = "Continue" # Default is SilentlyContinue
if($args.Count -ne 3){
Write-Error "args.count=$($args.Count). It should be 3"
return
}
$zip7FilePath="$PSScriptRoot\7zip\7za.exe"
$winrarFilePath="C:\Program Files\WinRAR\WinRAR.exe"
$solutionDir=$args[0] -replace "`n",""
$archivedFilePath=$args[1] -replace "`n",""
$installerFilePath=$args[2] -replace "`n",""
# Return true if all required data exist
function checkRequiredData ($solutionDir) {
Write-Debug "winrarFilePath=`"$winrarFilePath`""
Write-Debug "7zipFilePath=`"$zip7FilePath`""
if (!(Test-Path $winrarFilePath)){
# WinRAR does not exist
Write-Debug "`"$winrarFilePath`" doesn't exist, try to detect 7zip"
if (!(Test-Path $zip7FilePath)){
# 7za.exe does not exist
Write-Debug "`"$zip7FilePath`" doesn't exist, try to extract it from zip"
$url = "ftp://hanping-lab-pc/7zip.zip"
$output = "$PSScriptRoot\7zip.zip"
$start_time = Get-Date
if( !(Test-Path $output)){
# WinRAR does not exist
Write-Debug "`"$output`" doesn't exist, try to download it from hanping-lab-pc ftp"
Invoke-WebRequest -Uri $url -OutFile $output
Write-Debug "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
}
Expand-Archive -Path "$PSScriptRoot\7zip.zip" -DestinationPath "$PSScriptRoot"
if (!(Test-Path $zip7FilePath)){
Write-Error "`"$zip7FilePath`" doesn't exist"
return $false
}
}
}
Write-Debug "solutionDir=`"$solutionDir`""
# Check solution dir
if(!(Test-Path $solutionDir)){
Write-Error "solutionDir `"$solutionDir`" doesn't exist"
return $false
}
# Check Installer
Write-Debug "installerFilePath=`"$global:installerFilePath`""
if(!(Test-Path $global:installerFilePath)){
Write-Error "installerFilePath `"$global:installerFilePath`" doesn't exist"
return $false
}
return $true
}
function getMD5Hash($fileName) {
if([System.IO.File]::Exists($fileName)){
$fileStream = New-Object System.IO.FileStream($fileName,[System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::ReadWrite)
$md5Hash = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
[byte[]]$fileByteChecksum = $md5Hash.ComputeHash($fileStream)
$fileChecksum = ([System.Bitconverter]::ToString($fileByteChecksum)).Replace("-","")
$fileStream.Close()
} else {
$fileChecksum = "ERROR: $fileName Not Found"
}
return $fileChecksum
}
function archive () {
# Compress by WinRAR
$version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($global:installerFilePath).FileVersion
$archivedFilePath = [System.IO.Path]::GetDirectoryName($archivedFilePath) + "\\" + [System.IO.Path]::GetFileNameWithoutExtension($archivedFilePath) + "_$($version).zip"
# Remove existing archivedFile
Write-Debug "archivedFilePath=`"$archivedFilePath`""
if(Test-Path $archivedFilePath){
Remove-Item $archivedFilePath -Recurse
}
if ((Test-Path $winrarFilePath)){
# WinRAR does exist
$process = [System.Diagnostics.Process]::Start("$winrarFilePath", "a -m5 -afzip -ep1` `"$archivedFilePath`" `"$global:installerFilePath`"")
$process.WaitForExit()
}
elseif ((Test-Path $zip7FilePath)){
# 7-zip does exist
$process = [System.Diagnostics.Process]::Start("$zip7FilePath", "a -tzip `"$archivedFilePath`" `"$global:installerFilePath`"")
$process.WaitForExit()
}
# Save MD5 of archivedFilePath as text file
$md5Hash = (getMD5Hash($archivedFilePath)).toLower()
Write-Debug "MD5=`"$md5Hash`""
$dirName = [System.IO.Path]::GetDirectoryName($archivedFilePath)
$fileNameWithoutExt = [System.IO.Path]::GetFileNameWithoutExtension($archivedFilePath)
$md5FilePath = Join-Path -Path $dirName -ChildPath ("$($fileNameWithoutExt)_MD5.txt")
Write-Debug "md5FilePath=`"$md5FilePath`""
$md5Hash | Set-Content $md5FilePath
}
#main region
Write-Debug "solutionDir=`"$solutionDir`""
if(checkRequiredData($solutionDir) == $true){
archive
}
沒有留言:
張貼留言