Issue

A selected file should be stored in a ZIP file.


Solution

The .NET method CreateFromDirectory(String, String) in [IO.Compression.Zipfile] can be used to create a ZIP file containing all files in a directory:

Add-Type -assembly System.IO.Compression.Filesystem
 
$zipName = "{0}.zip" -f $file._Name
$workingDirectory = "C:\TEMP\$($file._Name)"
$sourceDirectory = Join-Path $workingDirectory "Source"
$destinationFullName = Join-Path $workingDirectory $zipName
 
if(Test-Path $workingDirectory) {
    Remove-Item -Path $workingDirectory -Force -Recurse -ErrorAction:SilentlyContinue
}
 
$downloadedFiles = Save-VaultFile -File $file._FullPath -DownloadDirectory $workingDirectory
$file = $downloadedFiles | select -First 1
$openResult = Open-Document -LocalFile $file.LocalPath -Options @{ FastOpen = $fastOpen } 
 
New-Item -Path $sourceDirectory -ItemType:Directory
Get-ChildItem $workingDirectory -File | % { Move-Item $_.FullName $sourceDirectory -Force }
 
[IO.Compression.Zipfile]::CreateFromDirectory($sourceDirectory, $destinationFullName)
  • Define the name of the ZIP file, in this case it's the name of the original file
  • Define a working directory where the original file will be stored when it is downloaded from Vault
  • Check if the working directory already exists. If it exists remove it and all subdirectories
  • Download the file from vault and save it to the working directory
  • Create the "source" subdirectory and move the downloaded file to the subdirectory
  • Create the ZIP file containing all files that are in the "source" directory

Remarks