Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace SlicingFile
- {
- // Write a program that takes any file and slices it to n parts. Write the following methods:
- // Slice(string sourceFile, string destinationDirectory, int parts) - slices the given source
- // file into n parts and saves them in destinationDirectory.
- // Assemble(List<string> files, string destinationDirectory) - combines all files into one, in the order they are passed,
- // and saves the result in destinationDirectory.
- public class somePublicClasses
- {
- // this stores the possition that we stopped writing the first file so it can start from the exact same possition for the next file
- public static long positionOfLastRead = 0;
- }
- class SlicingFile
- {
- static void Main()
- {
- // gets the source file name
- string sourceFile = @"..\..\VIDEO0011.mp4";
- // how many copies it should do
- int copies = int.Parse(Console.ReadLine());
- // this will be the destination of each slice
- string destination;
- List<string> destinations = new List<string>();
- for (int i = 0; i < copies; i++)
- {
- // adding each destination
- destination = @"..\..\Part" + i;
- // keeping all of the destinations for the merge
- destinations.Add(destination);
- Slice(sourceFile, destination, copies);
- }
- string newDestination = @"..\..\AssembledFile.mp4";
- Assemble(destinations, newDestination);
- }
- static void Slice(string sourceFile, string sliceDestination, int copies)
- {
- var originalFile = new FileStream(sourceFile, FileMode.Open);
- var destinationFile = new FileStream(sliceDestination, FileMode.Create);
- double maximumLengthToRead = (originalFile.Length / (double)copies);
- double maximumLenghtToReadPrecise = Math.Ceiling(maximumLengthToRead);
- if (originalFile.Length > 2000000000)
- {
- Console.WriteLine("The file is bigger than 2gb");
- return;
- }
- using (originalFile)
- {
- using (destinationFile)
- {
- byte [] buffer = new byte[originalFile.Length];
- // reads the original file
- int readBytes = originalFile.Read(buffer, 0 ,buffer.Length);
- destinationFile.Write(buffer, (int)somePublicClasses.positionOfLastRead,(int) maximumLenghtToReadPrecise);
- somePublicClasses.positionOfLastRead += (int)maximumLenghtToReadPrecise;
- }
- }
- }
- static void Assemble(List<string> files, string destinationDirectory)
- {
- for (int i =0; i < files.Count; i++)
- {
- var originalFile = new FileStream(files[i], FileMode.Open);
- var assembledFile = new FileStream(destinationDirectory, FileMode.Append);
- byte[] buffer = new byte[originalFile.Length];
- using(assembledFile)
- {
- using(originalFile)
- {
- int readBytes = originalFile.Read(buffer, 0, buffer.Length);
- assembledFile.Write(buffer,0,readBytes);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement