Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text.RegularExpressions;
- internal class SlicingFile
- {
- private static List<string> files = new List<string>();
- private static MatchCollection matches;
- private static void Main()
- {
- string inputFile = @"../../Rex.avi";
- string folderPath = @"../../";
- int NumberOfParts = 4;
- Slice(inputFile, folderPath, NumberOfParts);
- Assemble(files, folderPath);
- }
- private static void Assemble(List<string> files, string destinationDirectory)
- {
- // creating the file path for the reconstructed file
- string fileOutputPath = destinationDirectory + "assembled" + "." + matches[0].Groups[2];
- var fsSource = new FileStream(fileOutputPath, FileMode.Create);
- fsSource.Close();
- using (fsSource = new FileStream(fileOutputPath, FileMode.Append))
- {
- // reading the file paths of the parts from the files list
- foreach (var filePart in files)
- {
- using (var partSource = new FileStream(filePart, FileMode.Open))
- {
- // Create a byte array of the content of the current file
- Byte[] bytePart = new byte[partSource.Length];
- int readBytes = partSource.Read(bytePart, 0, bytePart.Length);
- // Write the bytes to the reconstructed file
- fsSource.Write(bytePart, 0, readBytes);
- }
- }
- }
- }
- private static void Slice(string sourceFile, string destinationDirectory, int parts)
- {
- using (var source = new FileStream(sourceFile, FileMode.Open))
- {
- long partSize = (long) Math.Ceiling((double) source.Length/parts);
- // The offset at which to start reading from the source file
- long fileOffset = 0;
- ;
- string currPartPath;
- FileStream fsPart;
- long sizeRemaining = source.Length;
- // extracting name and extension of the input file
- string pattern = @"(\w+)(?=\.)\.(?<=\.)(\w+)";
- Regex pairs = new Regex(pattern);
- matches = pairs.Matches(sourceFile);
- for (int i = 0; i < parts; i++)
- {
- currPartPath = destinationDirectory + matches[0].Groups[1] + String.Format(@"-{0}", i) + "." + matches[0].Groups[2];
- files.Add(currPartPath);
- // reading one part size
- using (fsPart = new FileStream(currPartPath, FileMode.Create))
- {
- byte[] buffer = new byte[partSize];
- int readBytes = source.Read(buffer, 0, buffer.Length);
- // creating one part size file
- fsPart.Write(buffer, 0, readBytes);
- }
- // calculating the remaining file size which iis still too be read
- sizeRemaining = (int) source.Length - (i*partSize);
- if (sizeRemaining < partSize)
- {
- partSize = sizeRemaining;
- }
- fileOffset += partSize;
- }
- }
- }
- public static Microsoft.Win32.SafeHandles.SafeFileHandle ImagePath { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement