compress-archive and preserve relative paths

前端 未结 4 2061
情书的邮戳
情书的邮戳 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:16

    The cumbersome technique mklement0 mentioned worked for me. Below is the script I created to support a list of various files mixed with folders.

    # Compress LFS based files into a zip
    # To use
    #  1. place this script in the root folder
    #  2. modify the contents of $lfsAssetFiles to point to files relative to this root folder
    #  3. modify $zipDestination to be where you want the resultant zip to be placed
    # based off of https://stackoverflow.com/a/51394271
    
    # this should match files being .gitignored
    $lfsAssetFiles = 
    "\Assets\Project\Plugins\x32",
    "\Assets\Project\Plugins\x64\HugePlugin.dll"
    
    # This is where the contents of the zip file will be structured, because placing them inside of a specific folder of the zip is difficult otherwise
    $zipStruct = $PSScriptRoot + "\zipStruct"
    
    # the actual zip file that will be created
    $zipDestination = "C:\Dropbox\GitLfsZip\ProjectNameLfs.zip"
    
    # remove files from previous runs of this script
    If(Test-path $zipStruct) {Remove-item $zipStruct -Recurse}
    If(Test-path $zipDestination) {Remove-item $zipDestination}
    
    Foreach ($entry in $lfsAssetFiles)
    {
      # form absolute path to source each file to be included in the zip
      $sourcePath = $PSScriptRoot + $entry;
    
      # get the parent directories of the path. If the entry itself is a directory, we still only need the parent as the directory will be created when it is copied over.
      $entryPath = Split-Path -Parent $entry
    
      # form what the path will look like in the destination
      $entryPath = $zipStruct + $entryPath
    
      # ensure the folders to the entry path exist
      $createdPath = New-Item -Force -ItemType Directory $entryPath
    
      # copy the file or directory
      Copy-Item -Recurse -Force $sourcePath $createdPath
    }
    
    # create a zip file https://blogs.technet.microsoft.com/heyscriptingguy/2015/page/59/
    Add-Type -AssemblyName "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory($zipStruct, $zipDestination)
    # Compress-Archive doesn't work here because it includes the "zipStruct" folder: Compress-Archive -Path $zipStruct -DestinationPath $zipDestination
    

提交回复
热议问题