Advertisement
bomman

Problem 5. Slicing File

Oct 3rd, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string source = "../../SOLID - Logger.avi";
  11.         string destination;
  12.         int n = int.Parse(Console.ReadLine());
  13.  
  14.         List<string> collectedDestinations = new List<string>(n);
  15.  
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             destination = "Part-" + i + ".avi";
  19.             collectedDestinations.Add(destination);
  20.             Slice(source, destination, n);
  21.         }
  22.  
  23.  
  24.         string newSource = "../../assembled.avi";
  25.         Assemble(collectedDestinations, newSource);
  26.     }
  27.  
  28.     static void Slice(string sourceFile, string destinationDirectory, int parts)
  29.     {
  30.         using (var source = new FileStream(sourceFile, FileMode.Open))
  31.         {  
  32.             using (var destination = new FileStream(destinationDirectory, FileMode.Create))
  33.             {
  34.                 double fileLength = source.Length;
  35.                 byte[] buffer = new byte[4096];
  36.                 int sumOfReadBytes= 0;
  37.                 while (sumOfReadBytes< fileLength/ parts)
  38.                 {
  39.                     int readBytes = source.Read(buffer, 0, buffer.Length);
  40.                     sumOfReadBytes+= readBytes;
  41.                     destination.Write(buffer, 0, readBytes);
  42.                 }
  43.             }
  44.         }          
  45.     }
  46.     static void Assemble(List<string> files, string destinationDirectory)
  47.     {
  48.         var assembledFile = new FileStream(destinationDirectory, FileMode.Append);
  49.         byte[] buffer = new byte[4096];
  50.  
  51.         using (assembledFile)
  52.         {
  53.             for (int i = 0; i < files.Count; i++)
  54.             {
  55.                 var opener = new FileStream(files[i], FileMode.Open);
  56.                 using (opener)
  57.                 {
  58.                     while (true)
  59.                     {
  60.                         int bytesRead = opener.Read(buffer, 0, buffer.Length);
  61.                         if (bytesRead == 0)
  62.                         {
  63.                             break;
  64.                         }
  65.                         assembledFile.Write(buffer, 0, bytesRead);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement