Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5.  
  6. namespace SlicingFile
  7. {
  8.     // Write a program that takes any file and slices it to n parts. Write the following methods:
  9.  
  10.     // Slice(string sourceFile, string destinationDirectory, int parts) - slices the given source
  11.     // file into n parts and saves them in destinationDirectory.
  12.  
  13.     // Assemble(List<string> files, string destinationDirectory) - combines all files into one, in the order they are passed,
  14.     // and saves the result in destinationDirectory.
  15.  
  16.     public class somePublicClasses
  17.     {
  18.         // this stores the possition that we stopped writing the first file so it can start from the exact same possition for the next file
  19.         public static long positionOfLastRead = 0;
  20.     }
  21.     class SlicingFile
  22.     {
  23.  
  24.         static void Main()
  25.         {
  26.             // gets the source file name
  27.             string sourceFile = @"..\..\VIDEO0011.mp4";
  28.             // how many copies it should do
  29.             int copies = int.Parse(Console.ReadLine());
  30.             // this will be the destination of each slice
  31.             string destination;
  32.  
  33.             List<string> destinations = new List<string>();
  34.  
  35.             for (int i = 0; i < copies; i++)
  36.             {
  37.                 // adding each destination
  38.                 destination = @"..\..\Part" + i;
  39.                 // keeping all of the destinations for the merge
  40.                 destinations.Add(destination);
  41.                 Slice(sourceFile, destination, copies);
  42.             }
  43.             string newDestination = @"..\..\AssembledFile.mp4";
  44.             Assemble(destinations, newDestination);
  45.         }
  46.  
  47.         static void Slice(string sourceFile, string sliceDestination, int copies)
  48.         {
  49.             var originalFile = new FileStream(sourceFile, FileMode.Open);
  50.             var destinationFile = new FileStream(sliceDestination, FileMode.Create);
  51.             double maximumLengthToRead = (originalFile.Length / (double)copies);
  52.             double maximumLenghtToReadPrecise = Math.Ceiling(maximumLengthToRead);
  53.             if (originalFile.Length > 2000000000)
  54.             {
  55.                 Console.WriteLine("The file is bigger than 2gb");
  56.                 return;
  57.             }
  58.             using (originalFile)
  59.             {
  60.                 using (destinationFile)
  61.                 {
  62.                     byte [] buffer = new byte[originalFile.Length];
  63.                     // reads the original file
  64.                     int readBytes = originalFile.Read(buffer, 0 ,buffer.Length);
  65.  
  66.                     destinationFile.Write(buffer, (int)somePublicClasses.positionOfLastRead,(int) maximumLenghtToReadPrecise);
  67.                     somePublicClasses.positionOfLastRead += (int)maximumLenghtToReadPrecise;
  68.                 }
  69.             }
  70.         }
  71.  
  72.         static void Assemble(List<string> files, string destinationDirectory)
  73.         {
  74.             for (int i =0; i < files.Count; i++)
  75.             {
  76.                 var originalFile = new FileStream(files[i], FileMode.Open);
  77.  
  78.                 var assembledFile = new FileStream(destinationDirectory, FileMode.Append);
  79.  
  80.                 byte[] buffer = new byte[originalFile.Length];
  81.  
  82.                 using(assembledFile)
  83.                 {
  84.                     using(originalFile)
  85.                     {
  86.                         int readBytes = originalFile.Read(buffer, 0, buffer.Length);
  87.                         assembledFile.Write(buffer,0,readBytes);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.        
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement