alexmitev

FileSliceAndAssemble

Dec 21st, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. //Problem 5.    Slicing File
  8. //Write a program that takes any file and slices it to n parts. Write the following methods:
  9. //•   Slice(string sourceFile, string destinationDirectory, int parts) - slices the given source file into n parts and saves them in destinationDirectory
  10. //•   Assemble(List<string> files, string destinationDirectory) - combines all files into one, in the order they are passed, and saves the result in destinationDirectory.
  11. namespace SplitFile
  12. {
  13.     class SplitInFiles
  14.     {
  15.         static void Assemble(List<string> files, string destinationDir)
  16.         {
  17.              int bufferSize = 4096 * 2;
  18.            
  19.              string extension = Path.GetExtension(files[0]);
  20.              
  21.              FileStream outputStream = new FileStream(string.Format("{0}assembled{1}", destinationDir, extension), FileMode.Append);
  22.            using (outputStream)
  23.            {
  24.                int index = 0;
  25.                byte[] buffer = new byte[bufferSize];
  26.                while (index < files.Count)
  27.                {
  28.                    using(var inputStreams = new FileStream (files[index], FileMode.Open))
  29.                    {
  30.                        while (true)
  31.                        {
  32.                            int bytes = inputStreams.Read(buffer, 0, buffer.Length);
  33.                            if (bytes == 0)
  34.                            {
  35.                                break;
  36.                            }
  37.                            outputStream.Write(buffer, 0, bytes);
  38.                        }
  39.                    }
  40.                    index++;
  41.                }
  42.            }
  43.  
  44.            
  45.         }
  46.         static void Slice(string sourceFile, string destinationDir, int parts)
  47.         {
  48.             FileStream inputStream = new FileStream(sourceFile, FileMode.Open);            
  49.             using (inputStream)
  50.             {
  51.                 string extension = Path.GetExtension(sourceFile);
  52.                 int bufferSize = 4096 * 2;
  53.                 byte[] buffer = new byte[bufferSize];
  54.                 int index = 1;
  55.                 long partSize = (long)Math.Ceiling( (double)inputStream.Length / parts);
  56.                 while (inputStream.Position < inputStream.Length)
  57.                 {
  58.                     using (var outputStream = new FileStream(string.Format("{0}part{1}{2}", destinationDir, index, extension), FileMode.Create))
  59.                     {
  60.                         long bytesRead = 0;
  61.  
  62.                         while (bytesRead < partSize)
  63.                         {
  64.  
  65.                             int bytes = inputStream.Read(buffer, 0, buffer.Length);
  66.                             if (bytes == 0)
  67.                             {
  68.                                 break;
  69.                             }
  70.                             outputStream.Write(buffer, 0, bytes);
  71.                             bytesRead += bytes;
  72.                         }
  73.                     }
  74.                     index++;
  75.                 }
  76.             }
  77.  
  78.         }
  79.  
  80.         static void Main()
  81.         {
  82.             var file = new List<string>() { @"../../Result/part1..mp4", @"../../Result/part2..mp4", @"../../Result/part3..mp4", @"../../Result/part4..mp4" };
  83.            
  84.             string sourceFile = @"../../Video.mp4";
  85.             string resultDirectory = @"../../Result/";
  86.             //Slice(sourceFile, resultDirectory, 4);
  87.             Assemble(file, resultDirectory);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment