Advertisement
Guest User

ConvertTo-Base64

a guest
Feb 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. function ConvertTo-Base64
  2. {
  3. param
  4. (
  5. [string] $SourceFilePath,
  6. [string] $TargetFilePath
  7. )
  8.  
  9. function Resolve-PathSafe
  10. {
  11. param
  12. (
  13. [string] $Path
  14. )
  15.  
  16. $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
  17. }
  18.  
  19. $SourceFilePath = Resolve-PathSafe $SourceFilePath
  20. $TargetFilePath = Resolve-PathSafe $TargetFilePath
  21.  
  22. $bufferSize = 9000 # should be a multiplier of 3
  23. $buffer = New-Object byte[] $bufferSize
  24.  
  25. $reader = [System.IO.File]::OpenRead($SourceFilePath)
  26. $writer = [System.IO.File]::CreateText($TargetFilePath)
  27.  
  28. $bytesRead = 0
  29. do
  30. {
  31. $bytesRead = $reader.Read($buffer, 0, $bufferSize);
  32. $writer.Write([Convert]::ToBase64String($buffer, 0, $bytesRead));
  33. } while ($bytesRead -eq $bufferSize);
  34.  
  35. $reader.Dispose()
  36. $writer.Dispose()
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement