[PowerShell] 将rar文件转换为zip格式
# Author: Icebird@cnblogs
# Purpose: RAR2ZIP
function Global:rar2zip($rarfile = $(throw "缺少参数"), $zipfile = "")
{
trap
{
$_.InvocationInfo
write-host ("{0,-17:S}{1} {2}" -f "Exception", ":", $_.Exception.Message)
break
}
function exist($path, $name)
{
return (dir $path | ? { $_.Name -eq $name } | measure-object).Count -gt 0
}
function deltree($path, $name)
{
if (exist "$path" "$name")
{
del "$path\$name" -recurse
}
}
$s = gp hklm:\Software\Classes\WinRAR\shell\open\command | findstr "(default)"
if ($s -eq $null)
{
return "请先安装WinRAR"
}
[regex] $regex = "`"(.+?)`""
$rar = ($regex.matches($s))[0].Groups[1].Value -replace "WinRAR.exe","RAR.exe"
$s = dir "$rarfile"
if ($s -eq $null)
{
return "$rarfile 不存在"
}
$tempdir = ${env:Temp} + "\rar2zip"
deltree ${env:Temp} "rar2zip"
md $tempdir > $null
&$rar x "$rarfile" "$tempdir"
if ($zipfile -eq "")
{
if ($rarfile -match ".rar$")
{
$zipfile = $rarfile -replace ".rar$",".zip"
}
else
{
$zipfile = $rarfile + ".zip"
}
}
pkzip25 -add -max -rec -path=relative "$zipfile" "$tempdir\*.*"
deltree ${env:Temp} "rar2zip"
}
# Purpose: RAR2ZIP
function Global:rar2zip($rarfile = $(throw "缺少参数"), $zipfile = "")
{
trap
{
$_.InvocationInfo
write-host ("{0,-17:S}{1} {2}" -f "Exception", ":", $_.Exception.Message)
break
}
function exist($path, $name)
{
return (dir $path | ? { $_.Name -eq $name } | measure-object).Count -gt 0
}
function deltree($path, $name)
{
if (exist "$path" "$name")
{
del "$path\$name" -recurse
}
}
$s = gp hklm:\Software\Classes\WinRAR\shell\open\command | findstr "(default)"
if ($s -eq $null)
{
return "请先安装WinRAR"
}
[regex] $regex = "`"(.+?)`""
$rar = ($regex.matches($s))[0].Groups[1].Value -replace "WinRAR.exe","RAR.exe"
$s = dir "$rarfile"
if ($s -eq $null)
{
return "$rarfile 不存在"
}
$tempdir = ${env:Temp} + "\rar2zip"
deltree ${env:Temp} "rar2zip"
md $tempdir > $null
&$rar x "$rarfile" "$tempdir"
if ($zipfile -eq "")
{
if ($rarfile -match ".rar$")
{
$zipfile = $rarfile -replace ".rar$",".zip"
}
else
{
$zipfile = $rarfile + ".zip"
}
}
pkzip25 -add -max -rec -path=relative "$zipfile" "$tempdir\*.*"
deltree ${env:Temp} "rar2zip"
}
这段脚本将增加一个全局函数rar2zip,用途是将rar文件转换成zip文件
注意:
1. 需要安装WinRAR
2. PKZIP25.EXE在搜索路径里的任意目录内
Usage:
rar2zip mydoc.rar
rar2zip mydoc.rar doc.zip
pkzip25.exe下载:
https://files.cnblogs.com/Icebird/PKZIP25.rar