compress-archive and preserve relative paths

前端 未结 4 2067
情书的邮戳
情书的邮戳 2020-12-06 06:38

I\'m having a challenging time getting compress-archive to do what I want...

I have a root project folder and I want to zip up some of the files in sub

4条回答
  •  渐次进展
    2020-12-06 07:10

    It appears that Compress-Archive (as of Windows PowerShell v5.1) doesn't support what you want:

    Targeting a folder recursively adds that folder's subtree to the archive, but only by the target folder's name (which becomes a child folder inside the archive), not its path.

    Specifically,

    Compress-Archive -Path scripts\module2 -DestinationPath tmp.zip
    

    will (recursively) store the contents of scripts\module2 in tmp.zip, but not with archive-internal path .\scripts\module2, just with .\module2 - the target folder's name (the last input path component).

    The implication is that you'd have to pass folder scripts instead to get the desired archive-internal path, but that would invariably include the entire subtree of scripts, given that Compress-Archive offers no inclusion/exclusion mechanism.


    One - cumbersome - option is to recreate the desired hierarchy in, say, the $env:TEMP folder, copy the target folder there, run Compress-Archive against the root of the recreated hierarchy, and then clean up:

    New-Item -Force -ItemType Directory $env:TEMP/scripts
    Copy-Item -Recurse -Force scripts/module2 $env:TEMP/scripts
    Compress-Archive -LiteralPath $env:TEMP/scripts -DestinationPath tmp.zip
    Remove-Item $env:TEMP/Scripts -Recurse -Whatif
    

    Otherwise, you may be able to find a solution:

    • by using the .NET v4.5+ [System.IO.Compression.ZipFile] class directly; you can load it into your session with Add-Type -Assembly System.IO.Compression.FileSystem (not necessary in PowerShell Core).

    • by using external programs such as 7-Zip,

提交回复
热议问题