Advertisement
alexmitev

SplitFileMethod

Dec 20th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. namespace SplitFile
  2. {
  3.     class SplitInFiles
  4.     {
  5.         static void Slice(string sourceFile, string destinationDir, int parts)
  6.         {
  7.             int bufferSize = 4096 * 4;
  8.             byte[] buffer = new byte[bufferSize];
  9.             FileStream inputStream = new FileStream(sourceFile, FileMode.Open);
  10.             using (inputStream)
  11.             {
  12.                 int index = 1;
  13.                 int partSize = (int)Math.Ceiling( (double)inputStream.Length / parts);
  14.                 while (inputStream.Position < inputStream.Length)
  15.                 {
  16.                     using (var outputStream = new FileStream(string.Format("{0}part{1}.mp4", destinationDir, index), FileMode.Create))
  17.                     {
  18.                         int bytesRead = 0;
  19.  
  20.                         while (bytesRead < partSize)
  21.                         {
  22.  
  23.                             int bytes = inputStream.Read(buffer, 0, (int)Math.Min(partSize - bytesRead, buffer.Length));
  24.                             if (bytes == 0)
  25.                             {
  26.                                 break;
  27.                             }
  28.                             outputStream.Write(buffer, 0, bytes);
  29.                             bytesRead += bytes;
  30.                         }
  31.                     }
  32.                     index++;
  33.                 }
  34.             }
  35.  
  36.         }
  37.  
  38.         static void Main()
  39.         {
  40.             string sourceFile = @"../../Video.mp4";
  41.             string resultDirectory = @"../../Result/";
  42.             Slice(sourceFile, resultDirectory, 4);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement