【轉自】http://www.cnblogs.com/Icebird/archive/2008/02/13/1068148.html
[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下載:
http://files.cnblogs.com/Icebird/PKZIP25.rar