Advertisement
Filkolev

Slicing File

May 25th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. namespace SlicingFile
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Linq;
  7.  
  8.     public class SlicingFile
  9.     {
  10.         public static void Main()
  11.         {
  12.             const string SourceFilePath = @"../../source.mp4";
  13.             const string DestinationFolder = @"../../Slices";
  14.  
  15.             Console.Write("Enter the number of slices: ");
  16.             int numberOfSlices = int.Parse(Console.ReadLine());
  17.  
  18.             Slice(SourceFilePath, DestinationFolder, numberOfSlices);
  19.  
  20.             List<string> fileNames = Directory.GetFiles(DestinationFolder).ToList();
  21.  
  22.             Assemble(fileNames, "../../");
  23.         }
  24.  
  25.         private static void Slice(string sourceFilePath, string destinationDirectory, int numberOfSlices)
  26.         {
  27.             using (var source = new FileStream(sourceFilePath, FileMode.Open))
  28.             {
  29.                 int bytesPerFile = (int)(source.Length / numberOfSlices);
  30.                 int remainingBytes = (int)(source.Length % numberOfSlices);
  31.                 string fileExtension = Path.GetExtension(source.Name);
  32.  
  33.                 for (int fileIndex = 0; fileIndex < numberOfSlices; fileIndex++)
  34.                 {
  35.                     string fileName = string.Format("File-{0}{1}", fileIndex, fileExtension);
  36.                     string destinationPath = destinationDirectory + "/" + fileName;
  37.  
  38.                     var bytesToWrite = fileIndex == numberOfSlices - 1 ? remainingBytes : bytesPerFile;
  39.  
  40.                     byte[] buffer = new byte[bytesToWrite];
  41.  
  42.                     using (var destination = new FileStream(destinationPath, FileMode.Create))
  43.                     {
  44.                         source.Read(buffer, 0, bytesToWrite);
  45.                         destination.Write(buffer, 0, bytesToWrite);
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private static void Assemble(List<string> files, string destinationDirectory)
  52.         {
  53.             const int DefaultBufferSizeInBytes = 4096;
  54.  
  55.             string extension = files[0].Substring(files[0].LastIndexOf('.'));
  56.             string path = destinationDirectory + "assembled" + extension;
  57.  
  58.             using (var assembled = new FileStream(path, FileMode.Append, FileAccess.Write))
  59.             {
  60.                 byte[] buffer = new byte[DefaultBufferSizeInBytes];
  61.  
  62.                 foreach (var file in files)
  63.                 {
  64.                     using (var slice = new FileStream(file, FileMode.Open))
  65.                     {
  66.                         while (true)
  67.                         {
  68.                             int bytesRead = slice.Read(buffer, 0, DefaultBufferSizeInBytes);
  69.  
  70.                             if (bytesRead == 0)
  71.                             {
  72.                                 break;
  73.                             }
  74.  
  75.                             assembled.Write(buffer, 0, bytesRead);
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement