poniedziałek, 10 września 2018

powershell - truncate binary file

I was forced to truncate file with tools available on system so I choose powershell - without additional downloads but almost pure powershell. Why I had to truncate file? Because file was uploaded and - by unknown reason - the header was wrong. In zip file the first two letters are "PK", but in my file it was as below (some unvisible characters also).
PS C:\temp> $bytes = [System.IO.File]::ReadAllBytes("c:\temp\some-file.zip")
PS C:\temp> $text = [System.Text.Encoding]::ASCII.GetString($bytes, 0, 12)
9? PK♥♦¶
 
PS c:\temp> $bytes.count
13908241
PS C:\temp> $bytes1 = $bytes[4..($bytes.Length-4)]
PS C:\temp> $bytes1.Count
13908234
PS C:\temp> $bytes1.length
13908234
PS C:\temp> [io.file]::WriteAllBytes('c:\temp\try1.zip', $bytes1)

Brak komentarzy:

Prześlij komentarz