Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- //Problem 5. Slicing File
- //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.
- namespace SplitFile
- {
- class SplitInFiles
- {
- static void Assemble(List<string> files, string destinationDir)
- {
- int bufferSize = 4096 * 2;
- string extension = Path.GetExtension(files[0]);
- FileStream outputStream = new FileStream(string.Format("{0}assembled{1}", destinationDir, extension), FileMode.Append);
- using (outputStream)
- {
- int index = 0;
- byte[] buffer = new byte[bufferSize];
- while (index < files.Count)
- {
- using(var inputStreams = new FileStream (files[index], FileMode.Open))
- {
- while (true)
- {
- int bytes = inputStreams.Read(buffer, 0, buffer.Length);
- if (bytes == 0)
- {
- break;
- }
- outputStream.Write(buffer, 0, bytes);
- }
- }
- index++;
- }
- }
- }
- static void Slice(string sourceFile, string destinationDir, int parts)
- {
- FileStream inputStream = new FileStream(sourceFile, FileMode.Open);
- using (inputStream)
- {
- string extension = Path.GetExtension(sourceFile);
- int bufferSize = 4096 * 2;
- byte[] buffer = new byte[bufferSize];
- int index = 1;
- long partSize = (long)Math.Ceiling( (double)inputStream.Length / parts);
- while (inputStream.Position < inputStream.Length)
- {
- using (var outputStream = new FileStream(string.Format("{0}part{1}{2}", destinationDir, index, extension), FileMode.Create))
- {
- long bytesRead = 0;
- while (bytesRead < partSize)
- {
- int bytes = inputStream.Read(buffer, 0, buffer.Length);
- if (bytes == 0)
- {
- break;
- }
- outputStream.Write(buffer, 0, bytes);
- bytesRead += bytes;
- }
- }
- index++;
- }
- }
- }
- static void Main()
- {
- var file = new List<string>() { @"../../Result/part1..mp4", @"../../Result/part2..mp4", @"../../Result/part3..mp4", @"../../Result/part4..mp4" };
- string sourceFile = @"../../Video.mp4";
- string resultDirectory = @"../../Result/";
- //Slice(sourceFile, resultDirectory, 4);
- Assemble(file, resultDirectory);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment