LoginSignup
1
0

Powershell でバイナリファイルを分割・復号

Posted at

はじめに

メイルを用いてファイルを共有する必要があった
しかしファイルサイズが大きくて送信できなかった
そんなこんなで分割と復号のPowershellスクリプト作ったので共有

分割

split.ps1
$src = join-path $(get-location) "source.zip"
$raw = [System.Io.File]::ReadAllBytes($src)
$b64 = [Convert]::ToBase64String($raw)
$size = 5MB
$num = 0
for ($i = 0; $i -lt $b64.Length; $i += $size){
	$chunk = $b64.Substring($i, [Math]::Min($size, $b64.Length - $i))
	$dst = $src + "-" + $num.ToString("D3") + ".txt"
	$chunk | Out-File -FilePath $dst
	$num++
}

結合

join.ps1
$prefix = "destination.zip"
$suffix = ".txt"

$b64 = ""
for ($i = 0; $i -lt 999; $i++) {
	$src = join-path $(get-location) ($prefix + '-' + $i.ToString("D3") + $suffix)
	if ($false -eq $(test-path $src)) {
		break
	}
	$b64 += get-content $src -raw
}
$raw = [Convert]::FromBase64String($b64)
$dst = join-path $(get-location) $prefix
set-content -path $dst -value $raw -encoding byte

おまけ

Powershellのスクリプトはたいていセキュリティで引っかかるので次のように実行すると吉
powershell -executionpolicy remotesigned .\split.ps1

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0