-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileCopier.cs
152 lines (125 loc) · 4.32 KB
/
FileCopier.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
namespace ConsoleInteractingFilesApp;
public sealed class FileCopier
{
#region Events about the start, progress, and completion of copying.
/// <summary>
/// Start of copying.
/// </summary>
public event EventHandler<EventArgs> CopyStarted;
/// <summary>
/// Calling the copy start event.
/// </summary>
private void OnCopyStarted()
{
CopyStarted?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Completion of the copy.
/// </summary>
public event EventHandler<EventArgs> CopyFinished;
/// <summary>
/// Calling the copy completion event.
/// </summary>
private void OnCopyFinished()
{
CopyFinished?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Copy progress.
/// </summary>
public event EventHandler<double> CopyProgress;
/// <summary>
/// Calling the copy progress event.
/// </summary>
/// <param name="progress"> Progress as a percentage. </param>
private void OnCopyProgress(double progress)
{
CopyProgress?.Invoke(this, progress);
}
#endregion
#region Methods of copying and writing data to a file.
/// <summary>
/// Byte-by-byte copying from one file to another.
/// </summary>
/// <param name="filePath"> The path of the original file. </param>
/// <param name="replaceFile"> Destination file path. </param>
/// <param name="blockSize"> Block Size. </param>
public void CopyFile(string filePath, string replaceFile, int blockSize)
{
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException(nameof(filePath), "The file name cannot be empty!");
}
if (string.IsNullOrEmpty(replaceFile))
{
throw new ArgumentNullException(nameof(replaceFile), "The file name cannot be empty!");
}
if (blockSize < 0)
{
throw new ArgumentOutOfRangeException(nameof(blockSize), "The value is incorrect!");
}
OnCopyStarted();
var bufferCount = 0;
using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
using (var reader = new BinaryReader(stream))
using (var destination = new FileStream(replaceFile, FileMode.OpenOrCreate))
using (var writer = new BinaryWriter(destination))
{
destination.SetLength(0);
while (reader.PeekChar() != -1)
{
var buffer = reader.ReadBytes(blockSize);
writer.Write(buffer);
OnCopyProgress((stream.Length + bufferCount * blockSize) / 10);
bufferCount++;
}
}
OnCopyFinished();
}
/// <summary>
/// Writing information to a file.
/// </summary>
/// <param name="path"> The file path. </param>
/// <param name="buffer"> The value to be written in bytes. </param>
/// <param name="count"> The number of recorded values. </param>
public void Write(string path, byte[] buffer, int count)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path), "The path cannot be empty!");
}
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer), "The value cannot be empty!");
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), "The number cannot be negative!");
}
using var stream = new FileStream(path, FileMode.OpenOrCreate);
stream.Write(buffer, 0, count);
}
/// <summary>
/// Writing data to the end of the file.
/// </summary>
/// <param name="path"> File path. </param>
/// <param name="buffer"> Data for recording. </param>
public void WriteEnd(string path, string buffer)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path), "The path cannot be empty!");
}
if (!File.Exists(path))
{
throw new FileNotFoundException("The file was not found!", nameof(path));
}
if (string.IsNullOrEmpty(buffer))
{
throw new ArgumentNullException(nameof(buffer), "The value cannot be empty!");
}
using var writer = new StreamWriter(path, true);
writer.Write(buffer);
}
#endregion
}