Skip to content

Commit 81bd815

Browse files
committed
Add CompressToBase64
1 parent 6b5761a commit 81bd815

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

Source/Private/CompressToBase64.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function CompressToBase64 {
2+
<#
3+
.SYNOPSIS
4+
Compresses and encodes a module file for embedding into a script
5+
.DESCRIPTION
6+
Reads the raw bytes and then compress (gzip) them, before base64 encoding the result
7+
.EXAMPLE
8+
Get-ChildItem *.dll, *.psm1 | CompressToBase64 -ExpandScript ImportBase64Module > Script.ps1
9+
10+
Script.ps1 will contain the base64 encoded (and compressed) contents of the files, piped to the ImportBase64Module function
11+
.LINK
12+
ImportBase64Module
13+
#>
14+
[CmdletBinding(DefaultParameterSetName = "Base64")]
15+
[OutputType([string])]
16+
param(
17+
# The path to the dll or script file to compress
18+
[Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
19+
[Alias("PSPath")]
20+
[string[]]$Path,
21+
22+
# If set, wraps the Base64 encoded content in the specified command
23+
[Parameter(Mandatory, Position = 1, ParameterSetName = "ExpandScriptName")]
24+
[string]$ExpandScriptName,
25+
26+
# If set, wraps the Base64 encoded content in the specified command
27+
[Parameter(Mandatory, Position = 1, ParameterSetName = "ExpandScript")]
28+
[ScriptBlock]$ExpandScript
29+
)
30+
begin {
31+
$Result = @()
32+
if ($ExpandScriptName -and !$ExpandScript) {
33+
$ExpandScript = (Get-Command $ExpandScriptName).ScriptBlock
34+
}
35+
}
36+
process {
37+
foreach ($File in $Path | Convert-Path) {
38+
$Source = [System.IO.MemoryStream][System.IO.File]::ReadAllBytes($File)
39+
$OutputStream = [System.IO.Compression.DeflateStream]::new(
40+
[System.IO.MemoryStream]::new(),
41+
[System.IO.Compression.CompressionMode]::Compress)
42+
$Source.CopyTo($OutputStream)
43+
$OutputStream.Flush()
44+
$ByteArray = $OutputStream.BaseStream.ToArray()
45+
if (!$ExpandScript) {
46+
[Convert]::ToBase64String($ByteArray)
47+
} else {
48+
$Result += [Convert]::ToBase64String($ByteArray)
49+
}
50+
}
51+
}
52+
end {
53+
if ($ExpandScript) {
54+
[ScriptBlock]::Create("@(`n'$($Result -join "'`n'")'`n)|.{`n${ExpandScript}`n}").ToString()
55+
}
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#! requires -Module ModuleBuilder
2+
Describe "CompressToBase64" {
3+
4+
Context "It compresses and encodes a file for embedding into a script" {
5+
BeforeAll {
6+
$Base64 = InModuleScope ModuleBuilder {
7+
CompressToBase64 $PSCommandPath
8+
}
9+
}
10+
11+
It "Returns a base64 encoded string" {
12+
$Base64 | Should -BeOfType [string]
13+
$Base64 | Should -Match "^[A-Za-z0-9\+\/]+=*$"
14+
$Base64.Length | Should -BeGreaterThan 0
15+
}
16+
17+
It "Returns the gzipped and encoded script" {
18+
$OutputStream = [System.IO.MemoryStream]::new()
19+
$InputStream = [System.IO.MemoryStream][System.Convert]::FromBase64String($Base64)
20+
$DeflateStream = [System.IO.Compression.DeflateStream]::new($InputStream, [System.IO.Compression.CompressionMode]::Decompress)
21+
$DeflateStream.CopyTo($OutputStream)
22+
$OutputStream.Seek(0, "Begin")
23+
$Source = [System.IO.StreamReader]::new($OutputStream, $true).ReadToEnd()
24+
25+
$Source | Should -Be (Get-Content $PSCommandPath -Raw)
26+
}
27+
}
28+
29+
Context "It wraps the Base64 encoded content in the specified command" {
30+
BeforeAll {
31+
$Base64 = InModuleScope ModuleBuilder {
32+
CompressToBase64 $PSCommandPath -ExpandScriptName ImportBase64Module
33+
}
34+
}
35+
36+
It "Returns a string" {
37+
$Base64 | Should -BeOfType [string]
38+
}
39+
40+
It "Pipes the encoding into the command" {
41+
$Block = InModuleScope ModuleBuilder {
42+
(Get-Command ImportBase64Module).ScriptBlock
43+
}
44+
45+
$Base64 | Should -Match "|.{`n$Block`n}$"
46+
}
47+
}
48+
49+
Context "It wraps the Base64 encoded content in the specified scriptblock" {
50+
BeforeAll {
51+
$Base64 = InModuleScope ModuleBuilder {
52+
Get-ChildItem $PSCommandPath | CompressToBase64 -ExpandScript { ImportBase64Module }
53+
}
54+
}
55+
56+
It "Returns a string" {
57+
$Base64 | Should -BeOfType [string]
58+
}
59+
It "Pipes the encoding into the scriptblock" {
60+
$Base64 | Should -Match "|.{`nImportBase64Module`n}$"
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)