-
-
Save JPMonglis/217822d6aa7dcd02ae9b40843e6313a2 to your computer and use it in GitHub Desktop.
function Compress-Data | |
{ | |
<# | |
.Synopsis | |
Compresses data | |
.Description | |
Compresses data into a GZipStream | |
.Link | |
Expand-Data | |
.Link | |
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx | |
.Example | |
$rawData = (Get-Command | Select-Object -ExpandProperty Name | Out-String) | |
$originalSize = $rawData.Length | |
$compressed = Compress-Data $rawData -As Byte | |
"$($compressed.Length / $originalSize)% Smaller [ Compressed size $($compressed.Length / 1kb)kb : Original Size $($originalSize /1kb)kb] " | |
Expand-Data -BinaryData $compressed | |
.Usage | |
.\compress_gzip.ps1 | |
$content = [IO.File]::ReadAllText("path to file") | |
Compress-Data $content -As String > gzip_compressed_payload | |
#> | |
[OutputType([String],[byte])] | |
[CmdletBinding(DefaultParameterSetName='String')] | |
param( | |
# A string to compress | |
[Parameter(ParameterSetName='String', | |
Position=0, | |
Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[string]$String, | |
# A byte array to compress. | |
[Parameter(ParameterSetName='Data', | |
Position=0, | |
Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[Byte[]]$Data, | |
# Determine how the data is returned. | |
# If set to byte, the data will be returned as a byte array. If set to string, it will be returned as a string. | |
[ValidateSet('String','Byte')] | |
[String]$As = 'string' | |
) | |
process { | |
if ($psCmdlet.ParameterSetName -eq 'String') { | |
$Data= foreach ($c in $string.ToCharArray()) { | |
$c -as [Byte] | |
} | |
} | |
#region Compress Data | |
$ms = New-Object IO.MemoryStream | |
$cs = New-Object System.IO.Compression.GZipStream ($ms, [Io.Compression.CompressionMode]"Compress") | |
$cs.Write($Data, 0, $Data.Length) | |
$cs.Close() | |
#endregion Compress Data | |
#region Output CompressedData | |
if ($as -eq 'Byte') { | |
$ms.ToArray() | |
} elseif ($as -eq 'string') { | |
[Convert]::ToBase64String($ms.ToArray()) | |
} | |
$ms.Close() | |
#endregion Output CompressedData | |
} | |
} |
Hey! Thank you for the script! Can you help me understand how to use it? In the code below I'm trying to gzip a single file.
This code creates a gzip archive, but inside it has a file without extension (just image
).
When I unpack it and add the png
extension the image viewer complains that the format is not supported.
Am I missing something?
$fileIn = 'C:\image.png'
$fileOut = 'C:\image.gz'
$originalData = Get-Content -Path $fileIn -Encoding Byte
$compressedData = Compress-Data -Data $originalData -As Byte
Set-Content $fileOut -Value $compressedData -Encoding Byte
Hey! Thank you for the script! Can you help me understand how to use it? In the code below I'm trying to gzip a single file. This code creates a gzip archive, but inside it has a file without extension (just
image
). When I unpack it and add thepng
extension the image viewer complains that the format is not supported. Am I missing something?$fileIn = 'C:\image.png' $fileOut = 'C:\image.gz' $originalData = Get-Content -Path $fileIn -Encoding Byte $compressedData = Compress-Data -Data $originalData -As Byte Set-Content $fileOut -Value $compressedData -Encoding Byte
Nevermind. Figured it out. The output file name should be changed to 'C:\image.png.gz'
Feel the need for choco ,
choco install gzip