前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >powershell:调用7z,haozip解压缩文件

powershell:调用7z,haozip解压缩文件

作者头像
10km
发布2018-01-03 14:20:09
3.5K0
发布2018-01-03 14:20:09
举报
文章被收录于专栏:10km的专栏10km的专栏

因为windows内置了zip格式文件的解压缩,所以powershell中可以直接调用powershell的函数就能实现zip解压缩.

利用powershell内置功能实现zip解压缩的代码如下:

# 调用powershell内置功能解压缩 $package 指定的 zip 文件到 $targetFolder
# 如果 $targetFolder为空则默认解压到 $package所在文件夹
function unzip([string]$zipFile,[string]$targetFolder){
    # 检查是否为zip后缀
    if ( Test-Path -Path $zipFile -PathType Leaf){
        echo "not found:$zipFile "
        exit -1
    }
    if(!$zipFile.ToLower().EndsWith(".zip")){
        echo "$zipFile not zip file"
        exit -1
    }
    # targetFolder为空时解压到zipFile同级文件夹的同名文件夹
    if(! $targetFolder){
        $targetFolder=(Get-Item $zipFile).Directory
    }    
    $shellApp = New-Object -ComObject Shell.Application
    $files = $shellApp.NameSpace($zipFile).Items()
    echo "unzip to $targetFolder..."    
    $shellApp.NameSpace($targetFolder).CopyHere($files)
}

但是在powershell下对其他的压缩格式(.7z,.tar,.gz,.rar….)就需要调用第三方工具来实现。 7z和HaoZip(好压)都提供了命令行解压缩工具,所以可以利用7z或HaoZip来实现 利用7z和HaoZip(好压)实现命令行解压的powershell实现代码如下: unpack.ps1

[CmdletBinding()]
param(
#[Parameter(Mandatory=$true,HelpMessage="输入要解压的压缩包名称(.zip,.gz...)")]
[string]$package,
[string]$targetFolder,
[switch]$quiet,
[switch]$help
)
# 上一条命令执行出错则中止脚本执行,并输出调用堆栈信息
function exit_on_error(){
    if ( ! $? ){
        echo "exit for error:$1 " 
        exit -1
    }
}
# 调用 haozip解压文件
function unpack_haozip([string]$exe,[string]$package,[string]$targetFolder){
    $item=Get-Item $exe    
    $unpack_exe=Join-Path -Path $item.Directory -ChildPath ('HaoZipC'+$item.Extension)

    $cmd="""$unpack_exe"" x $package -o$targetFolder -y"
    if( $quiet ){
        # -sn:禁止文字输出
        $cmd+=' -sn'
    }    
    cmd /c $cmd
    exit_on_error    
}
# 调用 7z解压文件
function unpack_7z([string]$exe,[string]$package,[string]$targetFolder){
    $item=Get-Item $exe
    $unpack_exe=Join-Path -Path $item.Directory -ChildPath ('7z'+$item.Extension)
    $cmd="""$unpack_exe"" x $package -o$targetFolder -y"
    if(! $quiet){
        # -bb[0-3] : set output log level
        $cmd+=' -bb1'
    }   
    cmd /c $cmd
    exit_on_error
    if( $package.ToLower().EndsWith('.tar.gz')){        
        $tar=Join-Path -Path $targetFolder -ChildPath (Get-Item $package).BaseName
        $cmd="""$unpack_exe"" x $tar -o$targetFolder -y"
        if(!$quiet){
            $cmd+=' -bb1'
        }   
        cmd /c $cmd
        exit_on_error
        del -Force -Recurse  $tar
        exit_on_error
    }
}

# 查看后缀为$suffix的文件的本机文件关联程序
function find_associated_exe([string]$suffix){
    $Extension,$FileType=(cmd /c assoc $suffix) -split '='
    if(!$FileType){
        Write-Host "请用手工指定 `$UNPACK_TOOL 变量指定解压缩软件,define `$UNPACK_TOOL to fix it"
        exit -1
    }    
    $FileType,$Executable= (cmd /c ftype $FileType) -split '='
    if( ! $Executable ){
        exit -1
    }
    # exe 全路径    
   ($Executable -replace '^([^"\s]+|"[^"]+?")(\s.+)?$','$1') -replace '(^"|"$)',''
}
# 为后缀为$suffix压缩包寻找解压缩工具
# 如果定义了 $UNPACK_TOOL 则优先使用它做为解压缩工具
# 否则 调用 assoc,ftype 来查找对应的解压缩工具,如果找不到就报错退出
function find_unpack_function([string]$suffix){
    if($UNPACK_TOOL){        
        $exe=$UNPACK_TOOL
    }else{
        $exe=find_associated_exe $suffix
    }
    $fun="unpack_"+ ((Get-Item $exe).BaseName.toLower() -replace '^.*(7z|haozip).*$','$1')
    # 返回解压缩函数名 unpack_xxxx
    $fun
    # 返回解压缩工具软件的exe文件(全路径)
    $exe
}
# 解压缩 $package 指定的文件到 $targetFolder
# 如果 $targetFolder为空则默认解压到 $package所在文件夹
function unpack([string]$package,[string]$targetFolder){
    if(! $targetFolder){
        $targetFolder=(Get-Item $package).Directory
    }
    $index=$package.LastIndexOf('.')
    if($index -lt 0){
        # 没有文件后缀,无法识别,报错退出
        echo "unkonw file fomat $package"
        exit -1
    }
    if(!( Test-Path -Path $targetFolder -PathType Container)){
        mkdir $targetFolder
        exit_on_error
    }
    $suffix=$package.Substring($index) 
    #if ( $suffix -eq '.zip' ){
    #    unzip $package $targetFolder
    #}else{        
        $fun,$exe=find_unpack_function $suffix
        # 调用 unpack_xxxx(haozip|7z)解压
        &$fun $exe $package $targetFolder
    #}
}
# 指定命令解压工具
# 这里指定的exe,是支持命令行运行的版本,
# 比如7z的 GUI版本的可执行文件是 7zfm.exe,命令行版本则是7z.exe
# 好压(HaoZip)的GUI版本的可执行文件是 HaoZip.exe,命令行版本则是 HaoZipC.exe
# 如果不设置此值,脚本会通过 assoc,ftype命令查找,但有可能查找不到
#$UNPACK_TOOL="C:\Program Files\7-Zip\7z.exe"
#$UNPACK_TOOL="C:\Program Files\2345Soft\HaoZip\HaoZipC.exe"
# 运行过程中是否显示显示详细的进行步骤
# 输出帮助信息
function print_help(){
    echo "用法: $my_name [可选项...][压缩包文件]
PowerShell解压文件工具

选项:
    -p,-package      要解压的文件(.zip,.tar,.gz...)
    -q,-quiet        不显示详细信息
    -h,-help        显示帮助信息
作者: guyadong@gdface.net
"
}
$my_name=$($(Get-Item $MyInvocation.MyCommand.Definition).Name)
if($help){
    print_help  
    exit 0
}
# 根据命令行参数对$package解压缩
unpack $package $targetFolder
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档