-
Notifications
You must be signed in to change notification settings - Fork 22
/
util.ps1
75 lines (64 loc) · 1.52 KB
/
util.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
function JoinFiles( [string] $outputPath, [string[]] $inputPaths )
{
$outFileName = split-path -leaf $outputPath
$outDir = split-path -parent $outputPath
$outDir = resolve-path $outDir
$outputPath = join-path $outDir $outFileName
$output = [IO.File]::Open( $outputPath, [IO.FileMode]::Create )
try
{
foreach ( $inputPath in $inputPaths )
{
$inputPath = resolve-path $inputPath
$input = [IO.File]::OpenRead( $inputPath )
$input.CopyTo( $output )
$input.Close()
}
}
finally
{
$output.Close()
}
}
function CompareFiles(
[string] $leftPath,
[string] $rightPath,
[int] $bufferSize = 0x4000 )
{
if ( $bufferSize -le 0 )
{
throw "Invalid buffer size"
}
$leftFile = new-object IO.FileInfo (resolve-path $leftPath)
$rightFile = new-object IO.FileInfo (resolve-path $rightPath)
if ( !$leftFile.Exists -or !$rightFile.Exists -or ($leftFile.Length -ne $rightFile.Length) )
{
return $false
}
$leftBuf = new-object byte[] $bufferSize
$rightBuf = new-object byte[] $bufferSize
try
{
$leftStream = $leftFile.OpenRead()
$rightStream = $rightFile.OpenRead()
do
{
$bytesRead = $leftStream.Read( $leftBuf, 0, $bufferSize )
[void] $rightStream.Read( $rightBuf, 0, $bufferSize )
for ( $i = 0; $i -lt $bytesRead; $i++ )
{
if ( $leftBuf[$i] -ne $rightBuf[$i] )
{
return $false
}
}
}
while ( $bytesRead -eq $bufferSize )
}
finally
{
$leftStream.Close()
$rightStream.Close()
}
return $true
}