**Edit two** - This code is an ugly, ugly kluge from olden days. You do not want it.
<!-- language-all: lang-csharp -->
This compresses the contents of `.\in` to `.\out.zip` with System.IO.Packaging.ZipPackage following the example [here][1]
$zipArchive = $pwd.path + "\out.zip"
[System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
$ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive,
[System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite")
$in = gci .\in | select -expand fullName
[array]$files = $in -replace "C:","" -replace "\\","/"
ForEach ($file In $files)
{
$partName=New-Object System.Uri($file, [System.UriKind]"Relative")
$part=$ZipPackage.CreatePart($partName, "application/zip",
[System.IO.Packaging.CompressionOption]"Maximum")
$bytes=[System.IO.File]::ReadAllBytes($file)
$stream=$part.GetStream()
$stream.Write($bytes, 0, $bytes.Length)
$stream.Close()
}
$ZipPackage.Close()
**Edit:** [Unreliable][2] for larger files, maybe >10mb, YMMV. [Something][3] [to do][4] with appdomain evidence and isolated storage. The friendlier .NET 4.5 [approach][5] works nicely from PS v3, but wanted more memory in my case. To use .NET 4 from PS v2, config files need an [unsupported][6] [tweak][7].
[1]:
[To see links please register here]
[2]:
[To see links please register here]
[3]:
[To see links please register here]
[4]:
[To see links please register here]
[5]:
[To see links please register here]
[6]:
[To see links please register here]
[7]:
[To see links please register here]