1. Appendices

Encode and compress the Sitecore license file

Version: 10.5

You must compress the Sitecore license file and encode it to Base64. This procedure processes the license file and stores the encoded license key in the .\secrets\sitecore-license.txt file.

To compress and encode the license file:

  1. Create a PowerShell command file, and enter the following function in it:
function ConvertTo-CompressedBase64String {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [ValidateScript({
            if (-Not ($_ | Test-Path)) {
                throw "The file or folder $_ does not exist"
            }
            if (-Not ($_ | Test-Path -PathType Leaf)) {
                throw "The Path argument must be a file. Folder paths are not allowed."
            }
            return $true
        })]
        [string]$Path
    )

    $fileBytes = [System.IO.File]::ReadAllBytes($Path)
    $memoryStream = New-Object System.IO.MemoryStream
    $gzipStream = New-Object System.IO.Compression.GzipStream(
        $memoryStream,
        [IO.Compression.CompressionMode]::Compress
    )
    
    $gzipStream.Write($fileBytes, 0, $fileBytes.Length)
    $gzipStream.Close()
    $memoryStream.Close()
    
    $compressedFileBytes = $memoryStream.ToArray()
    $encodedCompressedFileData = [Convert]::ToBase64String($compressedFileBytes)
    
    $gzipStream.Dispose()
    $memoryStream.Dispose()
    
    return $encodedCompressedFileData
}
  1. Run the PowerShell command file, specifying the path to the license file in the Path parameter. For example:
ConvertTo-CompressedBase64String -Path .\license.xml | Out-File -Encoding ascii -NoNewline -Confirm -FilePath .\secrets\sitecore-license.txt
If you have suggestions for improving this article, let us know!