【问题标题】:extract zip files silently using batch file使用批处理文件静默提取 zip 文件
【发布时间】:2016-05-09 10:48:43
【问题描述】:

我已经编写了以下 bat 文件来提取 zip 文件。但是当我从詹金斯执行它时,这不起作用。我怀疑这是因为它试图启动复制 UI 和服务正在阻止这样做,因为 Windows 服务不允许使用 UI。有没有办法编辑下面的脚本以静默解压缩?如果有其他工具,请提供一些示例。

    @echo off
FOR /D %%p IN ("%CD%\Setups\*.*") DO rmdir "%%p" /s /q

call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.15.190:8081/nexus/content/repositories/releases/ -Dartifact=k:update-service:1.0.3 -Ddest=Setups/Services/update-service.jar
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.15.190:8081/nexus/content/repositories/releases/ -Dartifact=k:installer-prerequisites:1.0.0 -Ddest=Setups/PreRequisites/installer-prerequisites.zip -Dpackaging=zip
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.15.190:8081/nexus/content/repositories/releases/ -Dartifact=k:-apps:1.0.0 -Ddest=Setups/Apps/-apps.zip -Dpackaging=zip
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.15.190:8081/nexus/content/repositories/releases/ -Dartifact=k:mosquitto:1.0.0 -Ddest=Setups/mosquitto/mosquitto.zip -Dpackaging=zip
call mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get -DrepoUrl=http://10.101.15.190:8081/nexus/content/repositories/releases/ -Dartifact=k:ble-service:1.0 -Ddest=Setups/Services/ble-service.jar
for /r %%i in ("*.zip") do (
    Call :UnZipFile "%%~dpi" "%%~fi"
    del /S /Q "%%~fi"
)
exit \b

:UnZipFile <ExtractTo> <newzipfile>
    setlocal
    set vbs="%temp%\_.vbs"
    if exist "%vbs%" del /f /q "%vbs%"
     >"%vbs%" echo Set fso = CreateObject("Scripting.FileSystemObject")
    >>"%vbs%" echo If NOT fso.FolderExists("%~1") Then
    >>"%vbs%" echo fso.CreateFolder("%~1")
    >>"%vbs%" echo End If
    >>"%vbs%" echo set objShell = CreateObject("Shell.Application")
    >>"%vbs%" echo set FilesInZip=objShell.NameSpace("%~2").items
    >>"%vbs%" echo objShell.NameSpace("%~1").CopyHere(FilesInZip)
    >>"%vbs%" echo Set fso = Nothing
    >>"%vbs%" echo Set objShell = Nothing
    cscript //nologo "%vbs%"
    if exist "%vbs%" del /f /q "%vbs%"
    endlocal

【问题讨论】:

  • 请使用命令行“解压”工具。有很多..
  • @Jayan 虽然有很多解压工具可以使用,但这并不意味着它们总是可以使用的。大部分时间使用批处理的原因意味着它必须与许多不同的 Windows 计算机兼容,这些计算机可能并不都有“解压缩工具”

标签: windows batch-file jenkins zip unzip


【解决方案1】:

您应该使用 7zip 工具。安装后,您应该使用以下命令。

"C:\Program Files\7-Zip\7z.exe" e "C:\myzipfile.7z" -o"C:\ExtractedFolder" *.* -r -y

或使用批处理文件进行参数化。

调用 "C:\Scripts\mycustombatch.bat" "%WORKSPACE%\myzipfile.7z" "C:\ExtractedFolder"

mycustombatch.bat

cd "C:\Program Files\7-Zip"
7z e %1 -o%2 *.* -r -y

7z.exe使用示例:http://www.dotnetperls.com/7-zip-examples

【讨论】:

    【解决方案2】:

    一种选择是改用 PowerShell 脚本,并使用如下内容:

    function UnZip
    {
        param([string]$zip, [string]$outpath)
    
        [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
    }
    UnZip "D:\YourFile.zip" "D:\The_Path_You_Want_It_To_Be_Extracted"
    

    这样您就不需要在构建服务器上安装任何第 3 方工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      相关资源
      最近更新 更多