-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathExpand-Data.ps1
74 lines (64 loc) · 4.39 KB
/
Expand-Data.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Expand-Data
{
<#
.Synopsis
Expands Compressed Data
.Description
Expands Compressed Data using the .NET GZipStream class
.Link
Compress-Data
.Link
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
.Example
Compress-Data -String ("abc" * 1kb) |
Expand-Data
#>
[CmdletBinding(DefaultParameterSetName='BinaryData')]
[OutputType([string],[byte])]
param(
# The compressed data, as a Base64 string
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true,Position=0,ParameterSetName='CompressedData')]
[string]
$CompressedData,
# The compressed data, as a byte array
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true,Position=0,ParameterSetName='BinaryData')]
[Byte[]]
$BinaryData,
# The type of data the decompressed object will be (a string or a byte array)
[ValidateSet('String', 'Byte')]
[string]
$As = 'String'
)
process {
#region Open Data
if ($psCmdlet.ParameterSetName -eq 'CompressedData') {
try {
$binaryData = [System.Convert]::FromBase64String($CompressedData)
} catch {
Write-Verbose "Unable to uncompress base 64 string"
return
}
}
$ms = New-Object System.IO.MemoryStream
$ms.Write($binaryData, 0, $binaryData.Length)
$ms.Seek(0,0) | Out-Null
$cs = New-Object System.IO.Compression.GZipStream($ms, [IO.Compression.CompressionMode]"Decompress")
#endregion Open Data
#region Compress And Render
if ($as -eq 'string') {
$sr = New-Object System.IO.StreamReader($cs)
$sr.ReadToEnd()
} else {
$bytes = do {
$byte = $cs.ReadByte()
if ($byte -ne -1) {
[Byte]$byte
} else {
break
}
} while ($byte -ne 1)
$bytes -as [byte[]]
}
#endregion Compress And Render
}
}