1

I am trying to base64 encode a ~66MB zip file to a string and write it to a file using Powershell. I'm working with a limitation that ultimately I have to include the base64 encoded file string directly into a Powershell script, such that when the script is run at a different location, the zip file can be recreated from it. I'm not limited to using Powershell to create the base64 encoded string. It's just what I'm most familiar with.

The code I'm currently using:

$file = 'C:\zipfile.zip'
$filebytes = Get-Content $file -Encoding byte
$fileBytesBase64 = [System.Convert]::ToBase64String($filebytes)
$fileBytesBase64 | Out-File 'C:\base64encodedString.txt'

Previously, the files I have worked with have been small enough that encoding was relatively fast. However, I am now finding that the file I'm encoding results in the process eating up all my RAM and ultimately is untenably slow. I get the feeling that there's a better way to do this, and would appreciate any suggestion.

  • 1
    [This](https://stackoverflow.com/a/57820500/4137916), which is C# but could be translated to PowerShell code fairly easily. Note that depending on how you embed the Base64 string in the script, it may be equally inefficient in recreating the file if special care is not taken. – Jeroen Mostert Mar 03 '21 at 18:37
  • @JeroenMostert , in Powershell it makes extremely slow performance in my case. Possibly because of `InputBlockSize` \ `OutputBlockSize` are too small and can not be changed. – filimonic Mar 03 '21 at 19:33

1 Answers1

1

Im my case it takes less than 1 second to encode or decode 117-Mb file.

Src file size: 117.22 MiB
Tgt file size: 156.3 MiB
Decoded size: 117.22 MiB
Encoding time: 0.294
Decoding time: 0.708

Code I making measures:

$pathSrc = 'D:\blend5\scena31.blend'
$pathTgt = 'D:\blend5\scena31.blend.b64'
$encoding = [System.Text.Encoding]::ASCII

$bytes = [System.IO.File]::ReadAllBytes($pathSrc)
Write-Host "Src file size: $([Math]::Round($bytes.Count / 1Mb,2)) MiB"
$swEncode = [System.Diagnostics.Stopwatch]::StartNew()
$B64String = [System.Convert]::ToBase64String($bytes, [System.Base64FormattingOptions]::None)
$swEncode.Stop()
[System.IO.File]::WriteAllText($pathTgt, $B64String, $encoding)

$B64String = [System.IO.File]::ReadAllText($pathTgt, $encoding)
Write-Host "Tgt file size: $([Math]::Round($B64String.Length / 1Mb,2)) MiB"
$swDecode = [System.Diagnostics.Stopwatch]::StartNew()
$bytes = [System.Convert]::FromBase64String($B64String)
$swDecode.Stop()
Write-Host "Decoded size: $([Math]::Round($bytes.Count / 1Mb,2)) MiB"

Write-Host "Encoding time: $([Math]::Round($swEncode.Elapsed.TotalSeconds,3)) s"
Write-Host "Decoding time: $([Math]::Round($swDecode.Elapsed.TotalSeconds,3)) s"
filimonic
  • 3,190
  • 1
  • 17
  • 24