-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathsplit-file.ps1
More file actions
61 lines (48 loc) · 1.48 KB
/
split-file.ps1
File metadata and controls
61 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Param(
[string] $Path,
[long] $ChunkSize = 1GB
)
$ErrorActionPreference = 'Stop'
if ($ChunkSize -le 0) {
Write-Error "Only positive sizes allowed"
exit 1
}
Write-Host "Splitting file $Path to parts of $ChunkSize byte size..."
[long] $bufferSize = 100MB
if ($bufferSize -gt $ChunkSize) {
$bufferSize = $ChunkSize
}
$buffer = New-Object byte[] $bufferSize
$sourceFile = [System.IO.File]::OpenRead($Path)
$sourceReader = New-Object System.IO.BinaryReader($sourceFile)
[int] $index = 1
do {
$filename = "$Path." + $index.ToString("000")
Write-Host "Writing $filename..."
$destFile = [System.IO.File]::Create($filename)
$destWriter = New-Object System.IO.BinaryWriter($destFile)
[long] $sizeSoFar = 0
[long] $bytesRead = 0
while ($sizeSoFar -lt $ChunkSize) {
[long] $sizeToRead = $ChunkSize - $sizeSoFar
if ($sizeToRead -gt $bufferSize) {
$sizeToRead = $bufferSize
}
Write-Verbose "Reading $sizeToRead bytes..."
$bytesRead = $sourceReader.Read($buffer, 0, $buffer.Length)
Write-Verbose "Read $bytesRead bytes."
if ($bytesRead -le 0) {
break;
}
$sizeSoFar += $bytesRead
$destWriter.Write($buffer, 0, $bytesRead)
}
$destWriter.Close()
$destFile.Close()
Write-Verbose "Wrote $bytesSoFar bytes to $filename."
$index++
} while ($bytesRead -gt 0)
$sourceReader.Close()
$sourceFile.Close()
Write-Host "Split complete."
exit $LASTEXITCODE