2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?