File Zip in native PowerShell with .NET 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Author: Rich Warren | |
Based on original c# code by Jon Galloway: | |
https://weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib | |
.DESCRIPTION | |
Tool for creating a Zip file in native Powershell with .NET 3.0 only. | |
You could always do this in PowerShell + .NET 4.5 with System.IO.Compression, but nothing worked for .NET 3.0/3.5. | |
.EXAMPLE | |
# Zip up a directory | |
CreateZipFromDirectory -ZipDir "c:\temp\files" -ZipFile "c:\temp\files.zip" | |
.EXAMPLE | |
# Create a zip file from a single input file | |
CreateZipFromFile -InputFile "c:\temp\files\test.txt" -ZipFile "c:\temp\files.zip" | |
.EXAMPLE | |
# Same as above, but multiple files.. | |
$myfiles = @("c:\temp\test.txt","C:\windows\system32\calc.exe","C:\windows\system32\notepad.exe") | |
Foreach ($file in $myfiles){ | |
CreateZipFromFile -InputFile $file -ZipFile "c:\temp\myfiles.zip" | |
} | |
#> | |
# Load WindowsBase.dll | |
[Reflection.Assembly]::LoadFile("c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll") | |
Function CreateZipFromDirectory{ | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$ZipDir, | |
[Parameter(Mandatory=$true)] | |
[string]$ZipFile | |
) | |
$zipper = [System.IO.Packaging.Package]::Open($ZipFile, [System.IO.FileMode]::OpenOrCreate) | |
$files = Get-ChildItem $ZipDir | |
Foreach ($file in $files) { | |
AddFileToZip -ZipFile $zipper -AddFile $file.FullName | |
} | |
$zipper.close() | |
} | |
Function CreateZipFromFile{ | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[string]$InputFile, | |
[Parameter(Mandatory=$true)] | |
[string]$ZipFile | |
) | |
$zipper = [System.IO.Packaging.Package]::Open($ZipFile, [System.IO.FileMode]::OpenOrCreate) | |
AddFileToZip -ZipFile $zipper -AddFile $InputFile | |
$zipper.close() | |
} | |
Function AddFileToZip{ | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
[System.IO.Packaging.Package]$ZipFile, | |
[Parameter(Mandatory=$true)] | |
[string]$AddFile | |
) | |
$destFilename = [System.IO.Path]::GetFileName($AddFile) | |
$uri = [System.IO.Packaging.PackUriHelper]::CreatePartUri($destFilename) | |
if ($ZipFile.PartExists($uri)){ | |
$ZipFile.DeletePart($uri) | |
} | |
$part = $ZipFile.CreatePart($uri, "", [System.IO.Packaging.CompressionOption]::Normal) | |
$InputStream = new-object IO.FileStream $AddFile, 'Open', 'Read' | |
$OutputStream = $part.GetStream() | |
$bufferSize = $InputStream.Length | |
$buffer = New-Object Byte[] $bufferSize | |
$bytesRead = 0; | |
$bytesWritten = 0; | |
while (($bytesRead = $InputStream.Read($buffer, 0, $buffer.Length)) -ne 0) | |
{ | |
$OutputStream.Write($buffer, 0, $bytesRead); | |
$bytesWritten += $bufferSize; | |
} | |
Write-Host "Bytes written: $bytesWritten" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment