PoshCode Archive  Artifact [38ee4f79f6]

Artifact 38ee4f79f6429daad9a8ce9b41fe7e3ed94f5a2b63e85dc8c3147aae5d977b03:

  • File New-ZipFile.ps1 — part of check-in [a589343eba] at 2018-06-10 13:07:00 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 2585)

# encoding: ascii
# api: powershell
# title: New-ZipFile.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: script
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2202
# x-archived: 2016-03-19T02:55:42
# x-published: 2011-09-09T21:42:00
#
#
##############################################################################
##
## New-ZipFile
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Create a Zip file from any files piped in. Requires that
you have the SharpZipLib installed, which is available from
http://www.icsharpcode.net/OpenSource/SharpZipLib/

.EXAMPLE

dir *.ps1 | New-ZipFile scripts.zip d:\bin\ICSharpCode.SharpZipLib.dll
Copies all PS1 files in the current directory to scripts.zip

.EXAMPLE

"readme.txt" | New-ZipFile docs.zip d:\bin\ICSharpCode.SharpZipLib.dll
Copies readme.txt to docs.zip

#>

param(
    ## The name of the zip archive to create
    $ZipName = $(throw "Specify a zip file name"),

    ## The path to ICSharpCode.SharpZipLib.dll
    $LibPath = $(throw "Specify the path to SharpZipLib.dll")
)

Set-StrictMode -Version Latest

## Load the Zip library
[void] [Reflection.Assembly]::LoadFile($libPath)
$namespace = "ICSharpCode.SharpZipLib.Zip.{0}"

## Create the Zip File
$zipName = $executionContext.SessionState.`
    Path.GetUnresolvedProviderPathFromPSPath($zipName)
$zipFile =
    New-Object ($namespace -f "ZipOutputStream") ([IO.File]::Create($zipName))
$zipFullName = (Resolve-Path $zipName).Path

[byte[]] $buffer = New-Object byte[] 4096

## Go through each file in the input, adding it to the Zip file
## specified
foreach($file in $input)
{
    ## Skip the current file if it is the zip file itself
    if($file.FullName -eq $zipFullName)
    {
        continue
    }

    ## Convert the path to a relative path, if it is under the
    ## current location
    $replacePath = [Regex]::Escape( (Get-Location).Path + "\" )
    $zipName = ([string] $file) -replace $replacePath,""

    ## Create the zip entry, and add it to the file
    $zipEntry = New-Object ($namespace -f "ZipEntry") $zipName
    $zipFile.PutNextEntry($zipEntry)

    $fileStream = [IO.File]::OpenRead($file.FullName)
    [ICSharpCode.SharpZipLib.Core.StreamUtils]::Copy(
        $fileStream, $zipFile, $buffer)
    $fileStream.Close()
}

## Close the file
$zipFile.Close()