Search notes:

System.IO.Compression.ZipFileExtensions::CreateEntryFromFile

The method CreateEntryFromFile, in class System.IO.Compression.ZipFileExtensions allows to add one file to a zip file and give this a file an arbitrary path.

PowerShell: Adding selected files from a filesystem tree

The following PowerShell function combines get-childItem with a script block to create zip files containing a sub-set of desired file of a directory tree.
The script block acts as a filter. Only files for which the script block returns $true are added to the zip file.
In the following invocation example, a zip file is created that only contains configuration files (*.config) of the directory src:
$filter = { $_.name -like '*.config' }

create-zip  ./src  $filter  configFiles.zip

Source code

add-type -assembly 'System.IO.Compression'
add-type -assembly 'System.IO.Compression.FileSystem'

set-strictMode -version latest

function create-zip {

   param (
      [string]      $dirRoot ,
      [scriptBlock] $callBack,
      [string]      $zipFile
   )

   if (test-path $zipFile) {
      remove-item $zipFile
   }

   [System.IO.Compression.ZipArchive] $zip = [System.IO.Compression.ZipFile]::Open(
        "$pwd/$zipFile",
       ([System.IO.Compression.ZipArchiveMode]::Create)
   )

   pushd $dirRoot

   get-childItem -attribute !directory -recurse |
   where-object $callBack |
   foreach-object {

       $fileToAdd         = $_.fullName
       $fileToAddRelative =  resolve-path $fileToAdd -relative

     # remove the dot slash (./) from the relative path:
       $entryName         = $fileToAddRelative.substring(2)

     # write-host "$fileToAddRelative -> $entryName"

       $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
           $zip,
           $fileToAdd,
           $entryName
       )
   }
   popd

   $zip.Dispose()
}

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1738303373, '18.218.119.140', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/dot-net/namespaces-classes/System/IO/Compression/ZipFileExtensions/CreateEntryFromFile(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78