Compressing Serialized Data

With Export-CliXml, it is trivial to serialize results to a file, and with Import-CliXml, the serialized information can just as easily be restored. However, the resulting XML files can become rather large.

Fortunately, in PowerShell 5 there is a new cmdlet called Compress-Archive. After you created the XML file, you could automatically refactor it into a ZIP file.

Here is some code to illustrate: it gets a process list and saves it to XML. Then, the XML file is crunched to ZIP, and the original XML file discarded.

This is highly efficient because XML is text. You often see compression rates around 3-5% (the ZIP file is 3-5% of the size of the original file):

$Path = "$env:TEMP\data1.xml"

# serialize data (results in large text files)
Get-Process | Export-Clixml -Path $Path

$length1 = (Get-Item -Path $Path).Length

$ZipPath = [IO.Path]::ChangeExtension($Path, ".zip")

Compress-Archive -Path $Path -Destination $ZipPath -CompressionLevel Optimal -Force
Remove-Item -Path $Path

$length2 = (Get-Item -Path $ZipPath).Length

$compression = $length2 * 100 / $length1
"Compression Ratio {0:n2} %" -f $compression 

Twitter This Tip! ReTweet this Tip!