-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathFileSplitter.ps1
87 lines (75 loc) · 2.24 KB
/
FileSplitter.ps1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function Split-File {
<#-- Another stackoverflow production
https://stackoverflow.com/questions/4533570/in-powershell-how-do-i-split-a-large-binary-file
--#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[String]
$InputFile,
[Parameter(Mandatory = $true)]
[String]
$OutDirectory,
[Parameter(Mandatory = $false)]
[String]
$OutputFilePrefix = "chunk",
[Parameter(Mandatory = $false)]
[Int32]
$ChunkSize = 1024
)
Begin {
Write-Output "Beginning to split your file.."
}
Process {
$FileStream = [System.IO.File]::OpenRead($InputFile)
$ByteChunks = New-Object byte[] $ChunkSize
$ChunkNumber = 1
While($BytesRead = $FileStream.Read($ByteChunks,0,$ChunkSize)) {
$OutputFile = "$OutputFilePrefix$ChunkNumber"
$OutputStream = [System.IO.File]::OpenWrite("$OutDirectory`\$OutputFile")
$OutputStream.Write($ByteChunks,0,$BytesRead)
$OutputStream.Close()
Write-Verbose "Wrote File $OutputFile"
$ChunkNumber += 1
}
}
End {
Write-Output "Finished splitting your file!"
}
}
function Reassemble-File {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[String]
$InputFileDirectory,
[Parameter(Mandatory = $true)]
[String]
$InputfilePrefix = "chunk",
[Parameter(Mandatory = $true)]
[String]
$OutputDirectory,
[Parameter(Mandatory = $true)]
[String]
$OutputFile
)
Begin {
Write-Output "Beginning to reassemble your files.."
}
Process {
$OutputStream = [System.Io.File]::OpenWrite("$OutputDirectory`\$OutputFile")
$ChunkNumber = 1
$InputFilename = "$InputFileDirectory`\$InputfilePrefix$ChunkNumber"
$Offset = 0
while(Test-Path $InputFilename) {
$FileBytes = [System.IO.File]::ReadAllBytes($InputFilename)
$OutputStream.Write($FileBytes, 0, $FileBytes.Count)
$ChunkNumber += 1
$InputFilename = "$InputFileDirectory`\$InputfilePrefix$ChunkNumber"
}
$OutputStream.close()
}
End {
Write-Output "Finished assembly!"
}
}