SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
59
in 23 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- static Stopwatch stopWatch;
- static void Main(string[] args)
- {
- string sourceFile = "C://KapRemont.rar";
- string sourceFileNew = "C://KapRemont.";
- FileInfo file = new FileInfo(sourceFile);
- stopWatch = new Stopwatch();
- stopWatch.Start();
- ////////////////////////////////////////////////////
- ReadFile(sourceFile, sourceFileNew + "1", file.Length);
- ////////////////////////////////////////////////////
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
- Console.WriteLine("RunTime " + elapsedTime);
- stopWatch.Start();
- ////////////////////////////////////////////////////
- ReadFileThread(sourceFile, sourceFileNew + "2", file.Length);
- ////////////////////////////////////////////////////
- stopWatch.Stop();
- ts = stopWatch.Elapsed;
- elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
- Console.WriteLine("RunTime " + elapsedTime);
- Console.ReadKey();
- }
- static int bufferSize = 1024 * 1024 * 50;
- static void ReadFile(string path, string pathNew, long length)
- {
- Console.WriteLine("Чтение начало");
- using (var a = File.Create(pathNew)) { }
- using (FileStream srcStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
- using (FileStream destStream = new FileStream(pathNew, FileMode.Create, FileAccess.Write, FileShare.Write))
- {
- srcStream.Position = 0;
- destStream.Position = 0;
- byte[] buffer = new byte[Math.Min(bufferSize, length)];
- while (length > 0)
- {
- int bytesRead = srcStream.Read(buffer, 0, bufferSize);
- if (bytesRead == 0) break;
- //destStream.Write(buffer, 0, bytesRead);
- length -= bytesRead;
- }
- }
- Console.WriteLine("Чтение конец");
- }
- static void ReadFileThread(string path, string pathNew, long length)
- {
- Console.WriteLine("Многопоточное чтение начало");
- using (var a = File.Create(pathNew)) { }
- int threadsCount = 1;
- long size = length / threadsCount;
- Thread[] threads = new Thread[threadsCount];
- for (int i = 0; i < threadsCount; i++)
- {
- long start = i * size;
- long partSize = size + (i == threadsCount - 1 ? length % threadsCount : 0);
- threads[i] = new Thread(() =>
- {
- WriteFileThreadPart(path, pathNew, start, partSize);
- });
- }
- foreach (Thread t in threads) t.Start();
- foreach (Thread t in threads) t.Join();
- Console.WriteLine("Многопоточное чтение конец");
- }
- static void WriteFileThreadPart(string srcName, string destName, long start, long partSize)
- {
- using (FileStream srcStream = new FileStream(srcName, FileMode.Open, FileAccess.Read, FileShare.Read))
- using (FileStream destStream = new FileStream(destName, FileMode.Create, FileAccess.Write, FileShare.Write))
- {
- srcStream.Position = start;
- destStream.Position = start;
- byte[] buffer = new byte[Math.Min(bufferSize, partSize)];
- while (partSize > 0)
- {
- int bytesRead = srcStream.Read(buffer, 0, bufferSize);
- if (bytesRead == 0) break;
- destStream.Write(buffer, 0, bytesRead);
- partSize -= bytesRead;
- }
- }
- }
RAW Paste Data

