daily pastebin goal
41%
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!
  1.         static Stopwatch stopWatch;
  2.         static void Main(string[] args)
  3.         {
  4.             string sourceFile = "C://KapRemont.rar";
  5.             string sourceFileNew = "C://KapRemont.";
  6.             FileInfo file = new FileInfo(sourceFile);
  7.  
  8.             stopWatch = new Stopwatch();
  9.             stopWatch.Start();
  10.             ////////////////////////////////////////////////////
  11.             ReadFile(sourceFile, sourceFileNew + "1", file.Length);
  12.             ////////////////////////////////////////////////////
  13.             stopWatch.Stop();
  14.             TimeSpan ts = stopWatch.Elapsed;
  15.             string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  16.             Console.WriteLine("RunTime " + elapsedTime);
  17.  
  18.             stopWatch.Start();
  19.             ////////////////////////////////////////////////////
  20.             ReadFileThread(sourceFile, sourceFileNew + "2", file.Length);
  21.             ////////////////////////////////////////////////////
  22.             stopWatch.Stop();
  23.             ts = stopWatch.Elapsed;
  24.             elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  25.             Console.WriteLine("RunTime " + elapsedTime);
  26.  
  27.             Console.ReadKey();
  28.         }
  29.  
  30.  
  31.  
  32.         static int bufferSize = 1024 * 1024 * 50;
  33.  
  34.         static void ReadFile(string path, string pathNew, long length)
  35.         {
  36.             Console.WriteLine("Чтение начало");
  37.  
  38.             using (var a = File.Create(pathNew)) { }
  39.  
  40.             using (FileStream srcStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
  41.             using (FileStream destStream = new FileStream(pathNew, FileMode.Create, FileAccess.Write, FileShare.Write))
  42.             {
  43.                 srcStream.Position = 0;
  44.                 destStream.Position = 0;
  45.  
  46.                 byte[] buffer = new byte[Math.Min(bufferSize, length)];
  47.                 while (length > 0)
  48.                 {
  49.                     int bytesRead = srcStream.Read(buffer, 0, bufferSize);
  50.                     if (bytesRead == 0) break;
  51.  
  52.                     //destStream.Write(buffer, 0, bytesRead);
  53.                     length -= bytesRead;
  54.                 }
  55.             }
  56.  
  57.             Console.WriteLine("Чтение конец");
  58.         }
  59.  
  60.         static void ReadFileThread(string path, string pathNew, long length)
  61.         {
  62.             Console.WriteLine("Многопоточное чтение начало");
  63.  
  64.             using (var a = File.Create(pathNew)) { }
  65.  
  66.             int threadsCount = 1;
  67.             long size = length / threadsCount;
  68.  
  69.             Thread[] threads = new Thread[threadsCount];
  70.             for (int i = 0; i < threadsCount; i++)
  71.             {
  72.                 long start = i * size;
  73.                 long partSize = size + (i == threadsCount - 1 ? length % threadsCount : 0);
  74.  
  75.                 threads[i] = new Thread(() =>
  76.                 {
  77.                     WriteFileThreadPart(path, pathNew, start, partSize);
  78.                 });
  79.             }
  80.  
  81.             foreach (Thread t in threads) t.Start();
  82.             foreach (Thread t in threads) t.Join();
  83.  
  84.             Console.WriteLine("Многопоточное чтение конец");
  85.         }
  86.  
  87.         static void WriteFileThreadPart(string srcName, string destName, long start, long partSize)
  88.         {
  89.             using (FileStream srcStream = new FileStream(srcName, FileMode.Open, FileAccess.Read, FileShare.Read))
  90.             using (FileStream destStream = new FileStream(destName, FileMode.Create, FileAccess.Write, FileShare.Write))
  91.             {
  92.                 srcStream.Position = start;
  93.                 destStream.Position = start;
  94.  
  95.                 byte[] buffer = new byte[Math.Min(bufferSize, partSize)];
  96.                 while (partSize > 0)
  97.                 {
  98.                     int bytesRead = srcStream.Read(buffer, 0, bufferSize);
  99.                     if (bytesRead == 0) break;
  100.  
  101.                     destStream.Write(buffer, 0, bytesRead);
  102.                     partSize -= bytesRead;
  103.                 }
  104.             }
  105.         }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top