-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeFile.ps1
98 lines (73 loc) · 2.46 KB
/
MergeFile.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
88
89
90
91
92
93
94
95
96
97
98
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
[string]$InputFirstFile
)
function Using-Object {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Object]
$InputObject,
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock
)
try {
. $ScriptBlock
}
finally {
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable]) {
$InputObject.Dispose()
}
}
}
function Merge-File {
param (
[parameter(Mandatory=$true)]
[string]$inputFirstFile
)
$fi = New-Object System.IO.FileInfo($inputFirstFile)
if (-not $fi.Exists) {
Write-Output "File not found"
return
}
if ($fi.Extension -ne ".p001") {
Write-Output "It's not first part"
return
}
[string]$outputFile = $inputFirstFile.Substring(0, $inputFirstFile.Length - 5)
[string]$nextFile = $inputFirstFile
[int]$i = 1
Using-Object ($fout = New-Object System.IO.FileStream($outputFile, [System.IO.FileMode]::Create)) {
[byte[]]$buffer = [System.Byte[]]::CreateInstance([System.Byte], 8 * 1024 * 1024)
[bool]$lastPart = $false
while ($true) {
Using-Object ($fin = New-Object System.IO.FileStream($nextFile, [System.IO.FileMode]::Open)) {
while ($true) {
[int]$bytesRead = $fin.Read($buffer, 0, $buffer.Length)
if ($bytesRead -eq 0) {
break
}
$fout.Write($buffer, 0, $bytesRead)
}
}
$i++
if($lastPart) {
break
}
$nextFile = $outputFile + (".p" + [System.String]::Format("{0:d3}", $i))
$nextfi = New-Object System.IO.FileInfo($nextFile)
if (-not $nextfi.Exists) {
break
}
if ($nextfi.Length -lt $fi.Length) {
$lastPart = $true
}
}
Write-Output ("Merged " + ($i - 1).ToString() + " parts")
}
}
Merge-File $InputFirstFile