【问题标题】:Powershell Compress-Archive files into foldersPowershell Compress-Archive 文件到文件夹中
【发布时间】:2018-04-26 14:28:27
【问题描述】:

如何将结果放入存档的文件夹中? 例如:

$Compress-Archive -Path .\Client\*.exe, .\Server\*.exe  -DestinationPath ('Build_' + (get-date -Format yyyy.MM.dd) + '.zip')

我想在存档中有一个包含 exe 文件的文件夹 Client 和一个包含 exe 文件的文件夹 Server。现在所有文件都直接位于存档的“根”中。

压缩整个文件夹时,文件夹名称包含在存档中。

【问题讨论】:

    标签: powershell compression


    【解决方案1】:

    这样的事情可能会让你从这里开始修改:-

    https://social.technet.microsoft.com/Forums/en-US/fb835e63-0ed5-417b-980b-46d2982f4e0b/add-a-folder-to-an-existing-zip-file-using-powershell-v4-or-lower?forum=winserverpowershell

    $sources = "c:\client","c:\server"
    $destination = "c:\test\" + "multifoler.zip"
    
    function Add-Zip
       {
        param([string]$zipfilename)
    
        if(-not (test-path($zipfilename)))
        {
            set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
            (dir $zipfilename).IsReadOnly = $false  
    }
    
    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)
    
    foreach($file in $input) 
    { 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
    }
    }
    
    foreach($source in $sources){
     Get-Item $source | Add-Zip $destination
    }
    

    【讨论】:

    • 只要压缩整个文件夹就可以了,但是当在 $sources 中使用通配符时,所有文件都在一个没有文件夹的文件中
    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 2021-10-23
    • 1970-01-01
    • 2021-07-11
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多