| external help file | Module Name | online version | schema |
|---|---|---|---|
PSCompression.dll-Help.xml |
PSCompression |
2.0.0 |
Compresses input strings into a Deflate-compressed Base64-encoded string.
ConvertTo-DeflateString
[-InputObject] <String[]>
[-Encoding <Encoding>]
[-CompressionLevel <CompressionLevel>]
[-AsByteStream]
[-NoNewLine]
[<CommonParameters>]The ConvertTo-DeflateString cmdlet compresses input strings into Deflate-compressed Base64-encoded strings or raw bytes using the DeflateStream Class. It is the counterpart to ConvertFrom-DeflateString.
PS ..\pwsh> $strings = 'hello', 'world', '!'
PS ..\pwsh> ConvertTo-DeflateString $strings
ykjNycnn5SrPL8pJ4eVS5OUCAAAA//8DAA==
# Or using pipeline input
PS ..\pwsh> $strings | ConvertTo-DeflateString
ykjNycnn5SrPL8pJ4eVS5OUCAAAA//8DAA==This example shows how to compress an array of strings into a single Deflate-compressed Base64 string, using either argument or pipeline input.
PS ..\pwsh> 'hello world!' | ConvertTo-DeflateString -AsByteStream | Set-Content -FilePath .\helloworld.deflate -AsByteStream
# To read the file back you can use `ConvertFrom-DeflateString` following these steps:
PS ..\pwsh> $path = Convert-Path .\helloworld.deflate
PS ..\pwsh> [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($path)) | ConvertFrom-DeflateString
hello world!This example shows how to use -AsByteStream to output raw compressed bytes that can be written to a file using Set-Content or Out-File. Note that the byte array is not enumerated.
Note
The example uses -AsByteStream with Set-Content, which is available in PowerShell 7+. In Windows PowerShell 5.1, use -Encoding Byte with Set-Content or Out-File to write the byte array to a file.
PS ..\pwsh> 'ñ' | ConvertTo-DeflateString -Encoding ansi | ConvertFrom-DeflateString
�
PS ..\pwsh> 'ñ' | ConvertTo-DeflateString -Encoding utf8BOM | ConvertFrom-DeflateString
ñThis example shows how different encodings affect the compression and decompression of special characters. The default encoding is utf8NoBOM.
# Check the total length of the files
PS ..\pwsh> (Get-Content myLogs\*.txt | Measure-Object Length -Sum).Sum / 1kb
87.216796875
# Check the total length after compression
PS ..\pwsh> (Get-Content myLogs\*.txt | ConvertTo-DeflateString).Length / 1kb
35.123456789This example demonstrates compressing the contents of multiple text files into a single Deflate-compressed Base64 string and compares the total length before and after compression.
Outputs the compressed byte array to the Success Stream.
Note
This parameter is intended for use with cmdlets that accept byte arrays, such as Out-File and Set-Content with -Encoding Byte (Windows PowerShell 5.1) or -AsByteStream (PowerShell 7+).
Type: SwitchParameter
Parameter Sets: (All)
Aliases: Raw
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies the compression level for the Deflate algorithm, balancing speed and compression size. See CompressionLevel Enum for details.
Type: CompressionLevel
Parameter Sets: (All)
Aliases:
Accepted values: Optimal, Fastest, NoCompression, SmallestSize
Required: False
Position: Named
Default value: Optimal
Accept pipeline input: False
Accept wildcard characters: FalseDetermines the character encoding used when compressing the input strings.
Note
The default encoding is UTF-8 without BOM.
Type: Encoding
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: utf8NoBOM
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies the input string or strings to compress.
Type: String[]
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: FalseThe encoded string representation of the input objects is concatenated to form the output. No newline character is added after each input string when this switch is used.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: FalseThis cmdlet supports the common parameters. For more information, see about_CommonParameters.
You can pipe one or more strings to this cmdlet.
By default, this cmdlet outputs a single Base64-encoded string.
When the -AsByteStream switch is used, this cmdlet outputs a byte array down the pipeline.