Skip to content

Commit

Permalink
feat(Base64.ps1): add Convert-ToBase64File function to encode file co…
Browse files Browse the repository at this point in the history
…ntent to Base64

The new function `Convert-ToBase64File` takes an input file path and an output file path, encodes the content of the input file to Base64, and writes the encoded content to the output file. This feature was added to facilitate file encoding operations within scripts.
  • Loading branch information
Bluzzi committed May 23, 2024
1 parent f6cda96 commit c24f825
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Core/Functions/Base64.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,34 @@ function atob {

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($string))
}

function Convert-ToBase64File {
param (
[Parameter(Mandatory=$true)]
[string]$InputFilePath,

[Parameter(Mandatory=$true)]
[string]$OutputFilePath
)

# Vérifier si le fichier d'entrée existe
if (-Not (Test-Path $InputFilePath)) {
Write-Error "Le fichier spécifié n'existe pas : $InputFilePath"
return
}

try {
# Lire le contenu du fichier d'entrée
$content = Get-Content -Path $InputFilePath -Raw

# Encoder le contenu en Base64
$encodedContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($content))

# Écrire le contenu encodé dans le fichier de sortie
Set-Content -Path $OutputFilePath -Value $encodedContent
Write-Host "Le fichier a été encodé et sauvegardé : $OutputFilePath"
}
catch {
Write-Error "Une erreur s'est produite lors de l'encodage ou de l'écriture du fichier : $_"
}
}

0 comments on commit c24f825

Please sign in to comment.