Search notes:

Powershell module: zip

The PowerShell module zip provides some basic functions for dealing with zip files.

Functions

new-zipArchive Creates a new zip archive, deletes it if it already exists
open-zipArchive Opens an existing archive
add-zipEntry Adds a file to the zip archive
close-zipArchive Closes an archive that was opened with new-zipArchive or open-zipArchive.

Example

$zip = new-zipArchive olfp-install-$(get-date -format 'yyyy-MM-dd.HH_mm').zip

foreach ($file in get-item abc/def/*.sql ) { add-zipEntry $file (resolve-path -relative $file) $zip }
foreach ($file in get-item ghi/jkl/*.sql ) { add-zipEntry $file (resolve-path -relative $file) $zip }
foreach ($file in get-item mno/readme.txt) { add-zipEntry $file (resolve-path -relative $file) $zip }
foreach ($file in get-item _install.sql  ) { add-zipEntry $file (resolve-path -relative $file) $zip }

close-zipArchive $zip

Source code

zip.psm1

set-strictMode -version latest

function new-zipArchive {
   param (
      [string]      $zipFilePath
   )

   $zipFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($zipFilePath)

   if (test-path $zipFilePath) {
      remove-item $zipFilePath
   }
   [System.IO.Compression.ZipArchive] $zip = [System.IO.Compression.ZipFile]::Open(
        "$zipFilePath",
       ([System.IO.Compression.ZipArchiveMode]::Create)
   )

   return $zip
}

function open-zipArchive {
 #
 # opens an (existing!) zip archive!
 #
   param (
      [string]      $zipFilePath
   )

   $zipFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($zipFilePath)

   if (-not (test-path $zipFilePath) ) {
      write-host "$zipFilePath does not exist"
      return
   }

   [System.IO.Compression.ZipArchive] $zip = [System.IO.Compression.ZipFile]::Open(
       $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($zipFilePath),
      ([System.IO.Compression.ZipArchiveMode]::Read)
   )

   return $zip
}

function add-zipEntry {
   param (
      [parameter(mandatory=$true) ]
      [string]                             $filePath     ,
       #
      [parameter(mandatory=$true )]
      [string]                             $entryName    , # = $filePath,
       #
      [parameter(mandatory=$true )]
      [System.IO.Compression.ZipArchive]   $zip            # = # $script:zip
   )

   $entryName = $entryName -replace '^\.[\\/]', ''
   $filePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($filePath)


   try {
     $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
             $zip,
             $filePath,
             $entryName
         )
   }
   catch {
      write-host "could not add $filePath"
   }
}

function close-zipArchive {
   param (
      [parameter(mandatory=$true)]
      [System.IO.Compression.ZipArchive]   $zip
   )

   $zip.Dispose()
}
Github repository ps-modules-zip, path: /zip.psm1

zip.psd1

@{

   RootModule        = 'zip.psm1'
   ModuleVersion     = '0.0.1'

   RequiredAssemblies = @(
     'System.IO.Compression',
     'System.IO.Compression.FileSystem'
   )

   RequiredModules   = @(
   )

   FunctionsToExport = @(
      'new-zipArchive'  ,
      'open-zipArchive' ,
      'add-zipEntry'    ,
      'close-zipArchive'
   )
   AliasesToExport   = @(
   )
}
Github repository ps-modules-zip, path: /zip.psd1

See also

René's simple PowerShell modules

Index