These 2 scripts are used to encode files in other files.
These 2 functions are used to include a file (scripts, binary, etc) as a resource in another text file. The latter is represented as a text header like this:
<# === BEGIN EMBEDDED FILE HEADER ===
H 4 s I A A A A A A A A C p 1 W X U / b M B R 9 r 9 T / c F V V a q K R a H t
F q g S U d m J j r K I V L 6 x C J r l t g l y 7 2 A 5 d B f z 3 X T s f N Q
y I h 0 C D 6 j M V E Y W q Q S t R a m x a T t / A F v B l 2 7 4 C A A A
=== END EMBEDDED FILE HEADER === #>
The function ConvertTo-HeaderBlock
takes a file path, then:
- get all the bytes from the file
- detect if the file is binary or text
- create a header including the file format and byte array size
- compress the byte array, including header using GZIP
- convert the data to Base 64 for text representation
- split the data in similar subgroups to have a pretty header block
All you need to do afterwards is to include that block of text whereever you want in the file of your choice.
NOTE THAT WHEN YOU EXTRACT THE DATA FROM THE FILE IT WAS INCLUDED IN, THE LOCATION OF THE TEXT BLOCK IS NOT IMPORTANT, CAN BE AT THE BEGINNING, MID or END OF FILE...
The function ConvertFrom-HeaderBlock
takes a file path, then:
- locate the text block that contains the resource in the file specified.
- convert Base64 to byte array
- decompress the bytes using GZIP
- read the header including the file format and byte array size
- get the raw byte array
- convert to text if required
. .\test\Test-ConvertScriptToHeader.ps1 -Verbose
Here's a fun test, this function will generate a header block based on a JPG image file and include that text in the script. The script will parse this text and extract the image the it uses in it's code.
. .\test\Test-RunImageLauncher.ps1
Takes 2 file and file bundle them together into one binary, compressed, data file. I contains the file data and their names and path. You can deserialized them in a separate folder or deserialize them o overwrite the original if you want.
$MyScript = "c:\script.ps1"
$DataFile = "c:\results.json"
# create an encoded file with the script and the results file
$SavedDataFile = New-EncodedFile -ScriptPath $MyScript -DataFilePath $DataFile
To get the files back from the encoded file
$null = mkdir "$pwd\out" -Force -ea Ignore
# extract in directory I specified
Restore-EncodedFiles -Path $SavedDataFile -DestinationPath "$pwd\out"
# extract and overwrite originals.
Restore-EncodedFiles -Path $SavedDataFile -OverwriteOriginalFiles
To show what the HEADER looks like...